题目大意
给定一个正整数,返回它在 Excel 表中相对应的列名称。
例如,
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
解题思路
该题可以看做是进制的转变,即十进制转为26进制,且0~25均用大写字母表示。
class Solution {
public:
string convertToTitle(int n) {
string ans = "";
while (n > 0)
{
char tmp = (n - 1) % 26 + 65;
ans = tmp + ans;
n = (n - 1) / 26;
}
return ans;
}
};