[LeetCode]171 Excel表列序号

题目描述

给定一个Excel表格中的列名称,返回其相应的列序号。

例如
A -> 1
B -> 2
C -> 3

Z -> 26
AA -> 27
AB -> 28

示例1

输入: "A"
输出: 1

示例2

输入: "AB"
输出: 28

示例3

输入: "ZY"
输出: 701

代码

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
length = len(s) - 1
num = 0
for i in s:
num += (ord(i) - 64) * 26 ** length
length -= 1
return num