leetcode【171】Excel Sheet Column Number

版权声明:本文为博主原创文章,未经允许,不得转载,如需转载请注明出处 https://blog.csdn.net/ssjdoudou/article/details/83620957

写在最前面:很简单的一道题,等airpods等的心急,写点简单的消遣消遣

leetcode【171】Excel Sheet Column Number

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

就类似于26进制,字符串对应数字最简单的就是利用ASCII码

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int
        """
        n = len(s)
        num = 0
        for i in range(n):
            num += (ord(s[i])-64)*(26**(n-i-1))
        return num

简单吧

猜你喜欢

转载自blog.csdn.net/ssjdoudou/article/details/83620957