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"))

 

相关文章:

  • 2021-11-08
  • 2021-07-27
  • 2021-12-29
  • 2021-11-05
  • 2021-06-20
  • 2021-12-20
  • 2021-12-10
猜你喜欢
  • 2021-12-14
  • 2021-09-18
  • 2022-01-29
  • 2021-12-20
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案