首页 > 代码库 > [leetcode] Excel Sheet Column Title

[leetcode] Excel Sheet Column Title

题目:(Math)

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 

题解:

已经想到就是10进制转换成26进制的问题了。但是还差一点。

例子可以多举一点,然后每个例子都实现一下。

public class Solution {    public String convertToTitle(int n) {        StringBuffer res = new StringBuffer();        while(n>0)        {           int r = n%26;           n =n/26;           if(r==0)           {               res.insert(0,‘Z‘);               n--;           }           else           {               res.insert(0,(char)(‘A‘+r-1));           }        }        return res.toString();    }}

 

[leetcode] Excel Sheet Column Title