原题链接https://leetcode.com/problems/excel-sheet-column-title/
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
题很简单,类似多维数组转一维数组。
为了方便,我的字典从0开始A~Z = 0~26
每次取余前 先将数减1.
class Solution {
public:
string convertToTitle(int n) {
string str = "";
while (n)
{
n--;
str = (char)(n % 26 + 'A') + str;
n /= 26;
}
return str;
}
};