LeetCode 168. Excel Sheet Column Title Excel表列名称

链接

https://leetcode-cn.com/problems/excel-sheet-column-title/description/

要求

给定一个正整数,返回它在 Excel 表中相对应的列名称。

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

相关代码

import string

class Solution:
    def convertToTitle(self, n):
        n_str = ''

        while n:
            if not n % 26:
                n_str = 'Z' + n_str
                n = n // 26 - 1

            elif n > 26:
                n_str = string.ascii_uppercase[n % 26 - 1] + n_str
                n = n // 26

            else:
                n_str = string.ascii_uppercase[n - 1] + n_str
                n = 0
                
        return n_str

心得体会

import string

print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)

# 输出结果
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

你可能感兴趣的:(LeetCode 168. Excel Sheet Column Title Excel表列名称)