描述
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
...
Example 1:
Input: "A"
Output: 1
Example 2:
Input: "AB"
Output: 28
Example 3:
Input: "ZY"
Output: 701
解析
根据题意,在将字母的二十六进制,转换为十进制,比较简单,时间复杂度为 O(N),空间复杂度为 O(1)。
解答
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.
每日格言:人生最大遗憾莫过于错误坚持和轻易放弃
请作者吃烟 支付宝