描述
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
您在真实的面试中是否遇到过这个题?
是
样例
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
相当于10进制转换26进制,但没有0,所以把n减一就更加方便了。
class Solution {
public:
/**
* @param n: a integer
* @return: return a string
*/
string convertToTitle(int n) {
// write your code here
if (n == 0) {
return "";
}
return convertToTitle((n - 1) / 26) + (char)((n - 1) % 26 + 'A');
}
};