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.

Solution: Excel Sheet Column Number, Python:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Date : 2015-01-02 19:57:57
# @Author : NSSimacer
# @Version : 1.0
class Solution:
# @param s, a string
# @return an integer
def titleToNumber(self, s):
result = 0
for char in s:
result = result * 26 + (ord(char) - ord('A') + 1)
return result

普通数学运算,也可以看成是26进制运算。

1
result += 26 ** (len(s) - i - 1) + (ord(s[i]) - ord('A') + 1)