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
<思路>主要是熟悉ord的用法,返回ascii码代表的数值。思路上比较简单,注意A的ascii是65,所以这里需要减去64。
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
sum = 0
for letter in s:
num = ord(letter)-64
sum = sum*26 + num
return sum

本文介绍了一种将Excel工作表中的列标题转换为其对应数字编号的算法。通过使用Python的ord函数,该算法能够处理从'A'到'ZZZ'的任何列标题,将它们转换为相应的数字编号,如'A'转为1,'AB'转为28等。
461

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



