題目來源
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
這個題目呢恩闻,一看呢伙菊,就感覺比較簡單,感覺就是一個10進(jìn)制轉(zhuǎn)26進(jìn)制的問題力穗。代碼如下:
class Solution {
public:
string convertToTitle(int n) {
string r;
while (n != 0) {
int x = (n-1) % 26;
r += char(x+65);
n = (n - 1) / 26;
}
reverse(r.begin(), r.end());
return r;
}
};