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
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases.
class Solution(object):
def convertToTitle(self, n):
res = ''
t = n % 26
if t == 0:
t = 26
n -= t
res += chr(t + ord('A') - 1)
while n >= 26:
#print n
n = n / 26
t = n % 26
if t == 0:
t = 26
res += chr(t + ord('A') - 1)
if n == 26:
break
#res.reverse()
return res[::-1]
"""
:type n: int
:rtype: str
"""

本文介绍了一种将正整数转换为Excel工作表中对应列标题的方法,例如1对应A,26对应Z,27对应AA等。通过递归算法处理特殊情况,如遇到26的倍数时进行特殊处理。

被折叠的 条评论
为什么被折叠?



