TypechoJoeTheme

IT技术分享

统计

[LeetCode 168] Excel Sheet Column Title [Java] [Runtime : 0MS]

2017-09-01
/
0 评论
/
886 阅读
/
正在检测是否收录...
09/01

1. Description

Given a positive integer, return its corresponding column title as appear in an Excel sheet.

2. Runtime Distribution

3. Submission Details

4. Example

1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB

5. Code

public String convertToTitle(int n) {
    StringBuilder sb = new StringBuilder();
    while (n > 0) {
        sb.insert(0, (char) ((n - 1) % 26 + 'A'));
        n = (n - 1) / 26;
    }
    return sb.toString();
}

6.Test

public class LeetCode0168 {
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            sb.insert(0, (char) ((n - 1) % 26 + 'A'));
            n = (n - 1) / 26;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LeetCode0168 leetcode = new LeetCode0168();
        System.out.println(leetcode.convertToTitle(2147483647));
    }
}
Math
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/784/(转载时请注明本文出处及文章链接)