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

請做者吃煙
支付寶

支付寶

微信

微信
相關文章
相關標籤/搜索