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 ...
Example 1:
Input: 1 Output: "A"
Example 2:
Input: 28 Output: "AB"
Example 3:
Input: 701 Output: "ZY"
代码:
class Solution {
public:
string convertToTitle(int n) {
string ans;
int cnt = 0;
while(n) {
if(n % 26 == 0) {
ans[cnt ++] = 'Z';
n -= 26;
} else {
ans[cnt ++] = 'A' - 1 + (n % 26);
n -= n % 26;
}
n /= 26;
}
for(int i = 0; i < cnt / 2; i ++)
swap(ans[i], ans[cnt - i - 1]);
return ans;
}
};
每次 $n$ 要减去 $n % 26$ 然后再 $n / 26$ 还有要注意的就是当 $n$ 可以被 $26$ 整除的时候要特别写一下 唉 这个题看了半天