美文网首页
LeetCode168. Excel Sheet Column

LeetCode168. Excel Sheet Column

作者: Yuu_CX | 来源:发表于2016-11-16 19:47 被阅读0次

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;
    }
};

相关文章

网友评论

      本文标题:LeetCode168. Excel Sheet Column

      本文链接:https://www.haomeiwen.com/subject/ldirpttx.html