题目详情:https://leetcode.com/problems/excel-sheet-column-title/description/
本来这道题挺简单的,按照进制转换的思路去做,没想到遇到n=52时候,出错了,没想明白,对针对n=52写了个处理过程。又出错了,这次出错的是n=702[衰]。后来发现这道题没有 “0”与之相对应啊,从1开始,26结束。举个例子:当n=52时,转换为”26进制”(平常所理解26进制是从0到25),为B0,这里的B等于2,即B*26+0*26^0=52,但是这里没有”0”,只能向前借位,前位减1,变为A,即(B-1=A),变化后为AZ,AZ就是52,因为(A*26^1+Z*26^0=26+26=52);同理n=702时,有”0”的话为AA0,但是没有,借位后为ZZ。
class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
s=''
while n>26:
m=n%26
n=n/26
print m,n
if m==0: #为0的情况需要特殊处理,借位处理
s='Z'+s #向前借一位
n=n-1#相应的前一位减1
elif chr(64+m)>='A':
s=chr(64+m)+s
return chr(64+n)+s