题目描述: 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
意思是将一个字符串转成相应的数字,其转换规则是 excel表格计数那样, A->1 AA->27
稍稍分析得知, 其实是一个26进制的问题, 容易得出: AA = 26^1*nums(A) + 26^0*nums(A) ABC= 26^2*nums(A)+26^1*nums(B)+....
代码如下:
class Solution(object): def titleToNumber(self, s): """ :type s: str :rtype: int """ l = len(s) num = 0 for i in range(0, l): num += (26**(l-i-1))*(ord(s[i])-64) return num