動機
高級的X進制
Problem
Given a string columnTitle
that represents the column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 ...
Example 1:
Input: columnTitle = AOutput: 1
Example 2:
Input: columnTitle = ABOutput: 28
Example 3:
Input: columnTitle = ZYOutput: 701
Example 4:
Input: columnTitle = FXSHRXWOutput: 2147483647
Constraints:
1 <= columnTitle.length <= 7
columnTitle
consists only of uppercase English letters.columnTitle
is in the range[A, FXSHRXW]
.
Sol
class Solution:
def titleToNumber(self, s: str) -> int:
mul = len(s)-1
s = list(s)
ret = 0
for c in s:
ret += (26**mul)*(ord(c)-ord('A')+1)
mul -= 1
return ret