标签:
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
可转化为求26进制的问题,有迭代和递归两种方式
class Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ n=len(s) #迭代方法 result=0 for i in range(n): result=result*26+ord(s[i])-64 return result #递归方法 if n == 1: return ord(s)-64 else: return ord(s[-1])-64 + 26*self.titleToNumber(s[:-1])
171. Excel Sheet Column Number
标签:
原文地址:http://www.cnblogs.com/sxbjdl/p/5280843.html