-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path171.excel表列序号.py
69 lines (63 loc) · 1.11 KB
/
171.excel表列序号.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# @lc app=leetcode.cn id=171 lang=python3
#
# [171] Excel表列序号
#
# https://leetcode-cn.com/problems/excel-sheet-column-number/description/
#
# algorithms
# Easy (65.81%)
# Likes: 117
# Dislikes: 0
# Total Accepted: 32.9K
# Total Submissions: 49.4K
# Testcase Example: '"A"'
#
# 给定一个Excel表格中的列名称,返回其相应的列序号。
#
# 例如,
#
# A -> 1
# B -> 2
# C -> 3
# ...
# Z -> 26
# AA -> 27
# AB -> 28
# ...
#
#
# 示例 1:
#
# 输入: "A"
# 输出: 1
#
#
# 示例 2:
#
# 输入: "AB"
# 输出: 28
#
#
# 示例 3:
#
# 输入: "ZY"
# 输出: 701
#
# 致谢:
# 特别感谢 @ts 添加此问题并创建所有测试用例。
#
#
# @lc code=start
class Solution:
def titleToNumber(self, s: str) -> int:
# import string
# d = {char: num for num, char in enumerate(string.ascii_uppercase, 1)}
res = 0
n = len(s)
for i in range(n):
# res += 26**(n - 1 - i) * d[s[i]]
# res = res * 26 + d[s[i]]
res = res * 26 + ord(s[i]) - 64
return res
# @lc code=end