leetcode 算法 Excel表列序号 python实现

这道题给我感觉就像一个26进制数一样。

A 就是1 B是2 。。。。 Z 是26

如果AB 两位,那就是  1 * 26 + 2   就是A 的数值*26 + B的数值

如果是MNP 三位数   那就是 M * 26^2 + N * 26^1 + P *26^0

就这样。。

 1 class Solution:
 2     def titleToNumber(self, s):
 3         """
 4         :type s: str
 5         :rtype: int
 6         """
 7         sum = 0
 8         li = [ord(i) - ord("A") + 1 for i in reversed(s)]
 9         for i in range(len(li)):
10             sum += li[i] * 26**i
11         return sum
12 
13 
14 
15 
16 if __name__ == '__main__':
17     s = Solution()
18     print(s.titleToNumber("ZY"))
原文地址:https://www.cnblogs.com/Lin-Yi/p/9595433.html