Leetcode 171 Excel Sheet Column Number python

时间:2023-03-08 15:37:26
Leetcode 171 Excel Sheet Column Number python

题目:

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 26进制
 class Solution(object):
def titleToNumber(self, s):
length = len(s)
if length == 0: return 0
ans = 0
for i in range(length):
ans = ans * 26 + ord(s[i]) - ord('A') + 1
return ans