Description
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
Code
class Solution:
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
if n == 0:
return ""
else:
return self.convertToTitle(int((n - 1) / 26)) + chr((n - 1) % 26 + ord('A'))
Conclusion
变型的进制问题。这里遇到个python3的坑,整数相除,得到的是浮点数了,不再取整了。