原题
Given a column title as appear in an Excel sheet, return its corresponding column number.
样例
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
解题思路
- 题目本质就是26进制转化为10进制
完整代码
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
for letter in s:
res = res * 26 + (ord(letter) - ord('A') + 1)
return res