题目:
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
方法一:性能59ms
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
res = ''
while n:
k = (n - 1) % 26
res = chr(k + 65) + res
n = (n - 1) / 26
return res
方法二:性能29ms
class Solution(object):
"""
:type n: int
:rtype: str
"""
def convertToTitle(self, n):
res = ""
while n >= 1:
n -= 1
res = chr(n%26 + ord("A")) + res
n = n/26
return res