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
...
Example 1:
Input: 1
Output: "A"
Example 2:
Input: 28
Output: "AB"
Example 3:
Input: 701
Output: "ZY"
<思路>学到python两个新方法ord():返回单个字符的ascii值(0-255)或者unicode数值。
>>> chr(100)
'd'
>>> chr(53)
'5'
和chr()方法,输入一个整数[0,255]返回其对应的ascii符号。
>>> chr(100)
'd'
>>> chr(53)
'5'
另外注意的是‘A’是从1开始的,所以一开始要减1再取余。‘A’的ascii值是65,所以要加上65。
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
column=''
while n>0:
n = n-1
column = chr(n%26+65)+column
n /= 26
return column