原题出处:https://leetcode.cn/leetbook/read/top-interview-questions-medium/xweb76/
解法一:
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
res = 0
for c in columnTitle:
res = res*26 + ord(c) - ord('A') + 1
return res
思路:
- 初始化结果为0
- 逐位遍历字符串columnTitle
- 对于每一位,将其转化为数字表示,即’A’=>1, ‘B’=>2,…,‘Z’=>26
- 将结果乘以26并加上当前位的数字表示
- 最后得到的结果即为对应的列序号