Special thanks to @ts for adding this problem and creating all test cases.
这题可以当作求26进制数来做。 可以用iterative的方法也可以用递归的方法来做
iterative:
class Solution:
# @param s, a string
# @return an integer
def titleToNumber(self, s):
result=0
n=len(s)
for i in range(n):
result=result*26+ord(s[i])-64
return resultclass Solution:
# @param s, a string
# @return an integer
def titleToNumber(self, s):
if len(s)==1:
return ord(s)-64
return ord(s[-1])-64+26*self.titleToNumber(s[:-1])171. Excel Sheet Column Number Leetcode Python
原文:http://blog.csdn.net/hyperbolechi/article/details/43828957