难度: Easy
原题连接
内容描述
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
思路 1 - 时间复杂度: O(N)- 空间复杂度: O(1)******
sb题没什么好说的,beats 32.58%
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
if not s or len(s) == 0:
return 0
res = 0
for i, c in enumerate(s):
res += (ord(c) - ord('A') + 1) * (26 ** (len(s) - i - 1))
return res