c++
class Solution {
public:
string convertToTitle(int n) {
string res = "";
while (n > 0) {
int tmp = n % 26 > 0 ? n % 26 : 26;
char ch = tmp - 1 + 'A';
res.push_back(ch);
n = (n-1)/26;
}
reverse(res.begin(), res.end());
return res;
}
};
python
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
res = []
while n>0:
ch = n % 26
if ch==0:
ch = 26
n = (n-1)//26
res.append(chr(ch -1 + ord('A')))
return ''.join(res[::-1])