171. Excel Sheet Column Number

# Easy

Key idea: 26^n

class Solution:
    def titleToNumber(self, s: str) -> int:
        res = 0
        i = 1
        for cha in reversed(s):
            res += i*(ord(cha)-64)
            i *= 26
        return res

ord() 函数是python内置函数,将char转换成int,比如ord(‘A')=65, ord('a')=97.

两个char类型在python里是无法直接加减运算的,必须转换成数字才行。

Last updated