首页 > 代码库 > Excel Sheet Column Title

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 

 其实就是10进制转26进制, 但是要注意的是A对应的是1,而不是0, 所以每次都要先-1。要倒过来输出。

 1 public class Solution { 2     public String convertToTitle(int n) { 3         StringBuilder sb = new StringBuilder(); 4             while(n > 0){ 5                 sb.insert(0,(char)((n - 1) % 26 + ‘A‘)); 6                 n = (int)(n - 1) / 26; 7             } 8             return sb.toString(); 9     }10 }

 

Excel Sheet Column Title