Skip to content

Latest commit

 

History

History
85 lines (45 loc) · 1.04 KB

0171._excel_sheet_column_number.md

File metadata and controls

85 lines (45 loc) · 1.04 KB

171. Excel Sheet Column Number

难度: Easy

刷题内容

原题连接

内容描述

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

解题方案

思路 1 - 时间复杂度: O(N)- 空间复杂度: O(1)******

sb题没什么好说的,beats 32.58%

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        if not s or len(s) == 0:
            return 0
        res = 0
        for i, c in enumerate(s):
            res += (ord(c) - ord('A') + 1) * (26 ** (len(s) - i - 1))
        return res