【LeetCode】168. Excel表列名称

版权声明:made by YYT https://blog.csdn.net/qq_37621506/article/details/83341295

1.题目

https://leetcode-cn.com/problems/excel-sheet-column-title/description/

2.思路

把数字/26像进制转化一样操作

3.代码

string convertToTitle(int n){
    string str="";
    int i,j=0;
    while(n)
    {
    i=(n-1)%26;
    str+='A'+i;
    n=(n-1)/26;
    }
    reverse(str.begin(),str.end());
    for(int i=0;i<str.length();i++)
    cout<<str[i];
    return str;
}

4.参考

class Solution {
public:
    string convertToTitle(int n) {
        string s;
        while(n){
            s += (n-1)%26+'A';
            n = (n-1)/26;
        }
        reverse(s.begin(), s.end());
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_37621506/article/details/83341295
今日推荐