168. Excel表列名称
https://leetcode-cn.com/problems/excel-sheet-column-title/
解法一:观察
其实就是26进制表示
"ZY" -- 701 = 26*26 + 1*25
"ZZ" -- 702 = 26*26 + 1*26
"AAA" -- 703 = 26^2*1 + 26*1 + 1*1
每次n%26得到最低位(1--A,...,25--Y,特别的是0--Z),然后n = n//26,进入下一轮,直到n==0,注意整除时(n%26==0)特殊,n = n//26 - 1。
class Solution:
def convertToTitle(self, n: int) -> str:
res = ""
while n != 0:
m = n % 26 #取模
if m == 0: #对模结果为0特殊处理
n = n // 26 - 1
res = 'Z' + res
else:
n = n // 26
res = chr(m + 64) + res #用asc码
return res
最后更新于