Given a column title as appear in an Excel sheet, return its corresponding column number.php
For example:ios
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
复制代码
Example 1:微信
Input: "A"
Output: 1
复制代码
Example 2:app
Input: "AB"
Output: 28
复制代码
Example 3:less
Input: "ZY"
Output: 701
复制代码
根据题意,在将字母的二十六进制,转换为十进制,比较简单,时间复杂度为 O(N),空间复杂度为 O(1)。yii
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
result = 0
count = 0
for i in s[::-1]:
result += 26**count*(ord(i)-ord('A')+1)
count += 1
return result
复制代码
Runtime: 20 ms, faster than 66.42% of Python online submissions for Excel Sheet Column Number.
Memory Usage: 11.7 MB, less than 41.88% of Python online submissions for Excel Sheet Column Number.
复制代码
每日格言:人生最大遗憾莫过于错误坚持和轻易放弃svg