LeetCode 168. [Python] Excel Sheet Column Title
原题链接
简单
作者:
徐辰潇
,
2021-02-05 03:18:14
,
所有人可见
,
阅读 612
class Solution:
def convertToTitle(self, n: int) -> str:
#TC: O(len(n))
#SC: O(1)
#'A' to 'Z': 1-26
# 'AA' to 'AZ': 1-26 + 1*26
#hex 26, any output, it is written as (i_1-1) + (i_2-1)*26 + (i_3-1)*26^2 + (i_4-4)*26^3 + .....
#Thus inorder to obtain i_j, just recursively divide 26 (i_j in range 1 to 26)
res = ""
while n:
remainder = (n-1) % 26
res = res + chr(remainder + ord('A'))
n = (n-1) // 26
return res[::-1]