题目:
Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
给定字串,回传它在excel表示行数的规则中代表什么数
example:
A -> 1
B -> 2
C -> 3
Z -> 26
AA -> 27
AB -> 28
这题是178.反过来操作而已
class Solution: def titleToNumber(self, columnTitle: str) -> int: ans=0 columnTitle=columnTitle[::-1] for i in range(len(columnTitle)): ans=ans+(ord(columnTitle[i])-64)*(26**i) return ans
先将字串反转比较好操作(从个位数开始)
从头开始,算出各位字母代表的数(透过ASCII转换)乘上26的n-1次方(n表第几位数)
全部加起来就是该字串代表的值
最后执行时间30ms(faster than 96.86%)
那我们下题见