题目:
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
题意:
给定一个非负整数,返回它对应的Excel 的列标题
代码:
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
res = []
while n/26 > 0 :
res.append( chr( (n-1)%26 + ord('A') ) )
n = (n-1)/26 #等于26时,Z不用进位,故用(n-1)/26
if n >= 1 : #有进位时,加上进位
res.append( chr( (n-1)%26 + ord('A') ) )
return ''.join(res[::-1]) #将得到字符串翻转,高位放在左边
笔记:
与10进制转化为16进制一样,但是全部用大写字母表示
1、字符转化为ASCLL码:ord('A')
2、ASCLL码转化为字符:chr(65)