LeetCode168. Excel Sheet Column

2016-11-16  本文已影响0人  Yuu_CX

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 
class Solution {
public:
    string convertToTitle(int n) {
        int res;
        string rr = "";
        while(n!=0){
            if(n%26==0){
                char c = 'Z';
                rr+=c;
                n/=26;
                n--;
            }else if(n%26!=0){
                res = n%26;
                char c = 'A'+res-1;
                rr+=c;
                n/=26;
            }
        }
        reverse(rr.begin(),rr.end());
        return rr;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读