问题描述:

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

例如,

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

示例 1:

输入: 1
输出: "A"

示例 2:

输入: 28
输出: "AB"

示例 3:

输入: 701
输出: "ZY"

方法1:

 class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
alph="0ABCDEFGHIJKLMNOPQRSTUVWXYZ" quo = 0
res=""
flag = False
while n != 0:
quo = n // 26
remainder = n % 26
n = quo
if remainder == 0:
res += ("Z")
flag = True
if quo == 1:
break
if quo == 27:#除数为702时,商27余0结果为zz
res += ("Z")
break
else:
if flag:
res += str(alph[remainder-1])
flag = False
else:
res += str(alph[remainder])
res = res[::-1]
return res

官方:chr(65)为A

 class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
result = ""
while n != 0:
result = chr((n-1)%26+65) + result n = (n-1)/26
return result

2018-09-14 21:01:38

LeetCode--168--Excel表列名称的更多相关文章

  1. LeetCode 168. Excel表列名称(Excel Sheet Column Title)

    168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...

  2. Java实现 LeetCode 168 Excel表列名称

    168. Excel表列名称 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -&g ...

  3. LeetCode 168. Excel表列名称(Excel Sheet Column Title)

    题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 - ...

  4. leetcode 实现-168.Excel表列名称

    168.Excel表列名称 描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C … 26 -> Z 27 -&g ...

  5. 刷题-力扣-168. Excel表列名称

    168. Excel表列名称 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/excel-sheet-column-title 著作权 ...

  6. 力扣168. Excel表列名称

    原题 1 class Solution: 2 def convertToTitle(self, n: int) -> str: 3 s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ...

  7. [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  8. LeetCode 171. Excel表列序号(Excel Sheet Column Number) 22

    171. Excel表列序号 171. Excel Sheet Column Number 题目描述 给定一个 Excel 表格中的列名称,返回其相应的列序号. 每日一算法2019/5/25Day 2 ...

  9. Java实现 LeetCode 171 Excel表列序号

    171. Excel表列序号 给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> ...

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

    给定一个正整数,返回它在Excel表中相对应的列名称.示例:    1 -> A    2 -> B    3 -> C    ...    26 -> Z    27 -&g ...

随机推荐

  1. Wireshark图解教程(简介、抓包、过滤器)(转)

    本文转自:http://www.cnblogs.com/observer/archive/2011/11/04/2235219.html 下面是一张地址为192.168.1.2的计算机正在访问&quo ...

  2. Linux基础命令---cpio

    cpio 从归档中复制文件,或者复制文件到归档中.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. Cpio命令有三种工作模式: 1)c ...

  3. vue v-for 和 v-if 、v-else一起使用造成的bug

    现象:导致v-else 执行v-for的length次数, 从现象看应该v-for先解析,然后将v-if和v-else包在其中 解决方案:很简单,tempalte 将v-if v-else 隔离到最外 ...

  4. Python3.x(windows系统)安装matplotlib库

    Python3.x(windows系统)安装matplotlib库 cmd命令: pip install matplotlib 执行结果:

  5. 20145101《Java程序设计》第9周学习总结

    20145101<Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增 ...

  6. Python3基础 super 子类调用父类的__init__

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. luogu P4396 [AHOI2013]作业

    目录 题目 思路 错误&&傻叉 代码 题目 luogu 思路 每次都是插入比之前所有数字大的数,所以之前的答案就不会改变 用fhq-treap求出原序列,然后用树状数组依次算出每个值得 ...

  8. Is it bad to rely on foreign key cascading? 外键 级联操作

    Is it bad to rely on foreign key cascading? I'll preface前言 this by saying that I rarely delete rows ...

  9. 使用CCleaner卸载chrome

    Google Chrome Update Patches Zero-Day Actively Exploited in the Wild 如果有同事使用google Chrome浏览器的话,请检查版本 ...

  10. MariaDB学习记录

    MariaDB的学习 MariaDB的学习 关于MariaDB的历史,不再概述 下面是mariadb的官网:https://mariadb.com/ 同样的,MariaDB一样有连接java的jar包 ...