Leetcode每日一题:168.excel-sheet-column-title(Excel表名称)

在这里插入图片描述
思路:这类似一个特殊的26进制转换,1-26对应’A’-‘Z’,将其减1后,0-25对应‘A’-‘Z’;
在这里插入图片描述
代码:

class Solution {
public:
string convertToTitle(int n)
    {
        string res = "";
        if(n==0)
            return res;
        int a = 0, b = 0;
        while (n > 0)
        {
            --n;
            a = n / 26;		//商
            b = n - a * 26; //余数
            res.insert(0,1,'A'+b);
            n = a;
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/wyll19980812/article/details/108095416
今日推荐