zoukankan      html  css  js  c++  java
  • LeetCode--168--Excel表列名称

    问题描述:

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

    例如,

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

    示例 1:

    输入: 1
    输出: "A"
    

    示例 2:

    输入: 28
    输出: "AB"
    

    示例 3:

    输入: 701
    输出: "ZY"
    

    方法1:

     1 class Solution(object):
     2     def convertToTitle(self, n):
     3         """
     4         :type n: int
     5         :rtype: str
     6         """
     7         alph="0ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     8         
     9         quo = 0
    10         res=""
    11         flag = False
    12         while n != 0:
    13             quo = n // 26
    14             remainder = n % 26
    15             n = quo
    16             if remainder == 0:
    17                 res += ("Z")
    18                 flag = True
    19                 if quo == 1:
    20                     break
    21                 if quo == 27:#除数为702时,商27余0结果为zz
    22                     res += ("Z")
    23                     break
    24             else:
    25                 if flag:
    26                     res += str(alph[remainder-1])
    27                     flag = False
    28                 else:
    29                     res += str(alph[remainder])
    30         res = res[::-1]
    31         return res

    官方:chr(65)为A

     1 class Solution(object):
     2     def convertToTitle(self, n):
     3         """
     4         :type n: int
     5         :rtype: str
     6         """
     7         result = ""
     8         while n != 0:
     9             result = chr((n-1)%26+65) + result
    10             
    11             n = (n-1)/26
    12         return result

    2018-09-14 21:01:38

  • 相关阅读:
    在Ubuntu_meta 16.04中设置默认Python3.5的命令
    树莓派安装中文输入法Fcitx及Google拼音输入法
    树莓派安装ubuntu_meta并配置开发环境
    业务代表模式
    MVC 模式
    访问者模式
    模板模式
    Linux进程调度与抢占
    IP地址
    策略模式
  • 原文地址:https://www.cnblogs.com/NPC-assange/p/9648941.html
Copyright © 2011-2022 走看看