LeetCode 171. Excel table column number (Python)

Topic Link

Subject description:

Given a column name of the Excel spreadsheet returns the corresponding column number.

E.g,

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:

Enter: "ZY"
output: 701

Problem-solving ideas:
as hex 26 can, for example, can be expressed as decimal 521: 521 = 5 10 ** 2 + 2 10 1 + 1 * 0
and then converted to a digital alphabet index is incremented by 1 when To .

class Solution(object):
    def titleToNumber(self, s):
        """
        :type s: str
        :rtype: int

        """
        s=s[::-1]
        num=0
        alp='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        for i in range(len(s)):
            j=alp.index(s[i])+1
            num+=j*26**i 
        return num

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44740082/article/details/91411529
Recommended