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();
    }
}

喜欢的话订阅一个呗~第一时间收到文章更新哟~

发表评论

电子邮件地址不会被公开。