LeetCode 168. Excel table column names (python)

Topic Link

Subject description:

Given a positive integer, it returns the name of the column corresponding to an Excel table.

E.g,

1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB 
...

Example 1:

Input: 1
Output: "A"
Example 2:

Input: 28
Output: "AB"
Example 3:

Input: 701
Output: "ZY"

Problem-solving ideas:

Excel this sequence is: A ~ Z, AA ~ ZZ, AAA ~ ZZZ, ......

Essentially convert a decimal to a hexadecimal number 26

Note: Since the index starts from 0 instead of starting from 1, therefore decremented by one.

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        res=''
        alp='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        while n>0:
            i=(n-1)%26
            res+=alp[i]
            n=(n-i)//26
        return res[::-1]

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44740082/article/details/91399953