給定一個正整數(shù),返回它在 Excel 表中相對應(yīng)的列名稱。
例如,
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
示例 1:
輸入: 1
輸出: "A"
示例 2:
輸入: 28
輸出: "AB"
示例 3:
輸入: 701
輸出: "ZY"
代碼
class Solution {
public:
string convertToTitle(int n) {
string res = "";
while (n) {
if (n % 26 == 0) {
res += 'Z';
n -= 26;
}
else {
res += n%26 - 1 + 'A';
n -= n%26;
}
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};