leetcode 171. Excel Sheet Column Number ( Python )

描述

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

请做者吃烟
支付宝

支付宝

微信

微信
相关文章
相关标签/搜索