写在最前面:很简单的一道题,等airpods等的心急,写点简单的消遣消遣
leetcode【171】Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Input: "ZY"
Output: 701
就类似于26进制,字符串对应数字最简单的就是利用ASCII码
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
n = len(s)
num = 0
for i in range(n):
num += (ord(s[i])-64)*(26**(n-i-1))
return num
简单吧


本文介绍了一种将Excel列标题转换为对应列号的算法实现,通过26进制方式,利用ASCII码将字符串映射到数字。示例代码展示了如何使用Python计算A至ZY等列标题的数值。

被折叠的 条评论
为什么被折叠?



