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 
http://blog.welkinlan.com/2015/10/17/excel-sheet-column-number-title-leetcode-java/
    public int titleToNumber(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            result = result * 26 + 1 + s.charAt(i) - 'A';
        }
        return result;
    }
http://blog.csdn.net/u012162613/article/details/42059591
因此可以将n转化为26进制表示的数,然后对每一位的数,根据『1->A,2->B,3->C....25->Y,26->Z』来转化。
当然,按照进制转换的方法(不断地除26取余数),不可能有26的余数,比如:52->(20)->AZ,此时余数是0,这种情况要特殊处理
string convertToTitle(int n) {
        string str;
        while(n){
            int r=n%26;
            n=n/26;
            if(r==0){   //为26的整数倍,该位设置为Z,n减掉1
                str+='Z';
                n--;
            }else{
                str+=('A'+r-1);
            }
        }
        //反转
        string result;
        for(int i=str.size()-1;i>=0;i--){
            result+=str[i];
        }
        return result;
    }http://www.programcreek.com/2014/03/leetcode-excel-sheet-column-title-java/
The key is
n--. The minimum in 26-bit number is mapped to 1, not 0.public String convertToTitle(int n) { StringBuilder result = new StringBuilder(); while(n>0){ n--; result.insert(0, (char)('A' + n % 26)); n /= 26; } return result.toString(); }
https://leetcode.com/discuss/34526/share-my-java-solusion
public String convertToTitle(int n) { String res = ""; while(n != 0) { res = (char)('A' + (n - 1) % 26) + res; n = (n - 1) / 26; } return res; }
https://leetcode.com/discuss/19047/my-1-lines-code-in-java-c-and-python
return n == 0 ? "" : convertToTitle(--n / 26) + (char)('A' + (n % 26));
This is a very neat solution, but I am afraid that the time complexity of this solution (at least in python, as far as I can tell) is O((n/26)^2) because of the string concatenation in python. The time complexity of string concatenation is O(k) since it is immutable, k is the total length of the string. In your case, the function recurs roughly n/26 times, and in each recursion, there is a O(n/26) operation. So the total complexity of this method is roughly O(n^2) instead of O(n).
An improve could be that only use list which has a O(1) complexity appending, and only concatenate once at the end. The time complexity will be O(n), but the code need more lines.
string convertToTitle(int n) { if (n == 0) { return ""; } return convertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A'); }http://blog.welkinlan.com/2015/10/17/excel-sheet-column-number-title-leetcode-java/
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
    1 -> A
    2 -> B
    3 -> C
    ...
    26 -> Z
    27 -> AA
    28 -> AB 
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            int d = n % 26;
            n /= 26;
            if (d == 0) {
                sb.append('Z');
                n--;
            } else {
                sb.append((char)('A' + d - 1));
            }
        }
        return sb.reverse().toString();
    }
https://leetcode.com/discuss/24116/the-way-i-come-up-with-my-solution