LeetCode刷题实战168:Excel表列名称
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
...
题意
示例 1:
输入: 1
输出: "A"
示例 2:
输入: 28
输出: "AB"
示例 3:
输入: 701
输出: "ZY"
解题
class Solution {
public:
string convertToTitle(int n) {
string ans;
unordered_map<int,char> m;
char ch = 'A';
for(int i = 0; i < 26; ++i)
m[i] = ch++;
while(n)
{
n--;
ans.push_back(m[n%26]);
n /= 26;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
赞 (0)