输入1 <= columnNumber <= 2
31 - 1,输出对应的excel的航标(A~Z字母表示的26进制)
写法一:用list存A~Z找对应
1 #168
2 #Runtime: 24 ms (Beats 5.91%)
3 #Memory: 13.4 MB (Beats 24.19%)
4
5 class Solution(object):
6 def convertToTitle(self, n):
7 """
8 :type n: int
9 :rtype: str
10 """
11 s = ""
12 lst = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 while n > 0:
14 t = n % 26
15 if t == 0:
16 t = 26
17 s = s + lst[t - 1]
18 n = (n - 1) // 26
19 #print(n)
20 return s[::-1]
写法二:用python自带函数chr
1 #168
2 #Runtime: 11 ms (Beats 82.44%)
3 #Memory: 13.2 MB (Beats 84.59%)
4
5 class Solution(object):
6 def convertToTitle(self, columnNumber):
7 """
8 :type columnNumber: int
9 :rtype: str
10 """
11 s = ""
12 while columnNumber > 0:
13 t = (columnNumber - 1) % 26
14 s = s + chr(t + 65)
15 columnNumber = (columnNumber - 1) // 26
16 return s[::-1]