-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path171.py
More file actions
26 lines (22 loc) · 909 Bytes
/
171.py
File metadata and controls
26 lines (22 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from functools import reduce
class Solution:
# 按照26进制数处理
def titleToNumber(self, s):
dict = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "H": 8, "I": 9, "J": 10, "K": 11,
"L": 12, "M": 13, "N": 14, "O": 15, "P": 16, "Q": 17, "R": 18, "S": 19, "T": 20, "U": 21,
"V": 22, "W": 23, "X": 24, "Y": 25, "Z": 26}
num = 0
for i in range(len(s)):
num = num + dict[s[i]] * pow(26, (len(s) - i - 1))
return num
def titleToNumber1(self, s):
s_len = len(s) - 1
res = 0
for idx, char in enumerate(s):
res += (ord(char) - 64) * 26 ** (s_len - idx)
return res
# Using reduce
def titleToNumber2(self, s):
return reduce(lambda x, y: x*26+y, map(lambda x: ord(x) - ord('A')+1, s))
if __name__ == "__main__":
print(Solution().titleToNumber2("ZY"))