168. Excel Sheet Column Title

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

For example:

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

 

class Solution {
    public String convertToTitle(int n) {
        String str = new String("ZABCDEFGHIJKLMNOPQRSTUVWXY");
        StringBuilder res = new StringBuilder();
        while(n>0){
            res.insert(0,String.valueOf(str.charAt(n%26)));
            if(n %26 == 0) n--;
            n /= 26;
        }
        return res.toString();
    }
}

本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注:

原文来源:《168. Excel Sheet Column Title》

172
0 0 172

延伸阅读

发表回复

登录后才能评论
分享本页
返回顶部