Given a column title as appear in an Excel sheet, return its corresponding column number.python
For example:bash
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
複製代碼
Example 1:app
Input: "A" Output: 1 Example 2:ui
Input: "AB" Output: 28 Example 3:spa
Input: "ZY" Output: 701code
思路:倒轉s,切片*26it
代碼:python3io
class Solution:
def titleToNumber(self, s: str) -> int:
revStr = s[::-1]
num=0
for i, ch in enumerate(revStr):
num = num+(ord(ch)-64)*pow(26, i)
return num
複製代碼