171. excel sheet column number

Description

Difficulty: Easy

Given a column title as appear in an Excel sheet, return its corresponding column number.

Example
A -> 1
B -> 2
C -> 3

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

将 Excel 中使用字母表示的列号转换为数字。

Solution

26 进制转换为 10 进制。

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