171. Excel Sheet Column Number
problem
Related to question Excel Sheet Column Title
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
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Show Tags
Show Similar Problems
solution
- 类似二十六进制转换成十进制
- 使用ord chr进行ascii转换
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
product = len(s)-1
num = 0
for x in s:
num += (ord(x)-64)*26**product
product -= 1
return num
discuss
一些更短小的写法,逻辑一样
return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in list(s)])
return reduce(lambda x,y:x*26+y,map(lambda x:ord(x)-ord('A')+1,s))
return reduce(lambda x, y: 26*x+ord(y)-64, s, 0)

浙公网安备 33010602011771号