168. Excel表列名称

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

例如,

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

输入: 1
输出: "A"
示例 2:

输入: 28
输出: "AB"
示例 3:

输入: 701
输出: "ZY"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/excel-sheet-column-title

明显用字典的水题、。。

class Solution:
    def convertToTitle(self, n: int) -> str:
        dict={i:chr(i+97).upper() for i in range(26)}
        res=[]
        while n:
            r=(n-1)%26
            res.append(dict[r])
            n=(n-1)//26
        return ''.join(res[::-1])

效率这么高是我没想到的

class Solution:
    def convertToTitle(self, n: int) -> str:
        dict={i:chr(i+97).upper() for i in range(26)}
        res=''
        while n:
            r=(n-1)%26
            res+=dict[r]
            n=(n-1)//26
        return res[::-1]

 不知道为什么换成字符串效率这么低、、、、

原文地址:https://www.cnblogs.com/xxxsans/p/13492314.html