[leedcode 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
public class Solution {
    public String convertToTitle(int n) {
        //本题是10进制转化为26进制,从结果的低位到高位计算(先取模再求余),注意n需要减一(780)
        //注意最后res字符串,需要头插法
        StringBuilder res=new StringBuilder();
        while(n>0){
            int t=(n-1)%26;
            res.insert(0,(char)('A'+t));
            n=(n-1)/26;
        }
        return res.toString();
       
    }
}

 

posted @ 2015-08-02 16:57  ~每天进步一点点~  阅读(123)  评论(0编辑  收藏  举报