• Leetcode练习(Python):数学类:第168题:Excel表列名称:给定一个正整数,返回它在 Excel 表中相对应的列名称。


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

    例如,

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

    思路:

    利用字典来做,思路较简单。

    程序:

    class Solution:
        def convertToTitle(self, n: int) -> str:
            myDict = {1: 'A',
                   2: 'B',
                   3: 'C',
                   4: 'D',
                   5: 'E',
                   6: 'F',
                   7: 'G',
                   8: 'H',
                   9: 'I',
                   10: 'J',
                   11: 'K',
                   12: 'L',
                   13: 'M',
                   14: 'N',
                   15: 'O',
                   16: 'P',
                   17: 'Q',
                   18: 'R',
                   19: 'S',
                   20: 'T',
                   21: 'U',
                   22: 'V',
                   23: 'W',
                   24: 'X',
                   25: 'Y',
                   26: 'Z',
                   }
            if n == 0:
                return ""
            result = ''
            while n > 26:
                auxiliary1 = n % 26
                if auxiliary1 == 0:
                    result = result + myDict[26]
                    n = n // 26 - 1
                else:
                    result = result + myDict[auxiliary1]
                    n = n // 26
            result = result + myDict[n]
            result = result[::-1]
            return result
  • 相关阅读:
    poj2954
    bzoj1863
    bzoj2002
    bzoj1389
    [POJ3041] Asteroids(最小点覆盖-匈牙利算法)
    [POJ2594] Treasure Exploration(最小路径覆盖-传递闭包 + 匈牙利算法)
    [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
    [luoguP1266] 速度限制(spfa)
    [luoguP1186] 玛丽卡(spfa)
    [luoguP1027] Car的旅行路线(Floyd)
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12836763.html
Copyright © 2020-2023  润新知