首页 > 代码库 > Excel Sheet Column Title

Excel Sheet Column Title

Excel Sheet Column Title

 

Given a non-zero 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 

参考:http://www.cnblogs.com/ganganloveu/p/4175848.html
这道题以为很easy,但还是参考了别人的才A出来了
java中能用StringBuffer还是用StringBuffer,感觉StringBuffer要比String要好用一点
 1 public class Solution { 2    public String convertToTitle(int n) { 3         StringBuffer result = new StringBuffer(); 4         while(n > 0){ 5             n--; 6             int temp = n % 26 + 65; 7             result.append((char)temp); 8             n = n / 26; 9             10         }11         for(int i = 0, j = result.length() - 1; i < j;i++, j--){12             char temp = result.charAt(i);13             result.setCharAt(i, result.charAt(j));14             result.setCharAt(j, temp);15             16         }17         return result.toString();18     }19 }

 

Excel Sheet Column Title