美文网首页
38Excel表列名称

38Excel表列名称

作者: Jachin111 | 来源:发表于2020-08-18 12:57 被阅读0次

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

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

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

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

示例 3:
输入: 701
输出: "ZY"

十进制转26进制

class Solution:
    def convertToTitle(self, n: int) -> str:
        res = ""
        while n:
            n, y = divmod(n, 26) 
            if y == 0:
                n -= 1
                y = 26
            res = chr(y + 64) + res
        return res
class Solution:
    def convertToTitle(self, n: int) -> str:
        res = ""
        while n:
            n -= 1
            n, y = divmod(n, 26) 
            res = chr(y + 65) + res
        return res

递归法

class Solution:
    def convertToTitle(self, n: int) -> str:
        return "" if n == 0 else self.convertToTitle((n - 1) // 26) + chr((n - 1) % 26 + 65)

来源:力扣(LeetCode)

相关文章

  • 38Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如,1 -> A2 -> B3 -> C...26 ...

  • Excel 表列序号、Excel表列名称

    给你一个字符串 columnTitle,表示 Excel 表格中的列名称。返回该列名称对应的列序号。 例如 示例 ...

  • Excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如,1 -> A2 -> B3 -> C...26 ...

  • Excel表列名称

    题目 难度级别:简单 给定一个正整数,返回它在 Excel 表中相对应的列名称。 例如, 示例 1: 输入: 1输...

  • LeetCode.168 & 171 Excel

    168. Excel表列名称 171. Excel表列序号 168:看似是一个进制转换的问题,但是要注意用的是字符...

  • Python算法-进制转换

    168. Excel表列名称[https://leetcode-cn.com/problems/excel-she...

  • 168. Excel Sheet Column Title

    excel表列名称,给定整数 columnNumber,返回在 Excel表中相对应的列名称。 时间复杂度 O(N...

  • [LeetCode]168. Excel表列名称

    168. Excel表列名称给定一个正整数,返回它在 Excel 表中相对应的列名称。例如,1 -> A2 -> ...

  • T168、excel表列名称

    给定一个正整数,返回它在 Excel 表中相对应的列名称。例如, 示例 1:输入: 1输出: "A"示例 2:输入...

  • LeetCode 168.Excel表列名称

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

网友评论

      本文标题:38Excel表列名称

      本文链接:https://www.haomeiwen.com/subject/xhfqjktx.html