Leetcode168. Excel Sheet Column Title
来源:互联网 发布:凯斯西储大学轴承数据 编辑:程序博客网 时间:2024/05/22 10:49
Probelm Description
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB
Thinking:
这个问题是 Easy 型的,也就是比较简单,但是如果不想清楚,很难做对。因为这个题目比较别扭的是列数计数不是从 0 开始的,而是从1开始的,比如从 26(Z) 到 27(AA) , 如果直接看成 26 进制的数的话, 会发现Z(26) 加1直接变成了AA(11),而不是我们习惯的(10)。所以需要特别注意一下。
我们先来看一个字符串表示的数字好了,用
比如对于字符 ZZZ 表示的整数为
那么 ZZZ 的下一列,也就是 AAAA =
Python Code:
由上面的分析可以看到,可以不断地除以26,得到的余数就是当前位,不过需要注意的是,当余数为0的时候,说明当前位表示的数应该是26(Z),这时候要把商减去1。
class Solution(object): def convertToTitle(self, n): """ :type n: int :rtype: str """ resStr = "" numMod = n while True: if numMod <= 0: break numMod, numRem = divmod(numMod, 26) if numRem == 0: numMod -= 1 resStr = "Z"+resStr else: resStr = chr(64+numRem)+resStr return resStr
0 0
- Leetcode168. Excel Sheet Column Title
- LeetCode168 Excel Sheet Column Title
- LeetCode168——Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- Excel Sheet Column Title
- 正则表达重点笔记
- 尚硅谷_Banking项目的那些事
- poj 1062 昂贵的聘礼(DFS)
- 子fragment中加载listview
- 利用photoshop设计艺术字
- Leetcode168. Excel Sheet Column Title
- hdu5699货物运输
- 位运算
- SQL语法手册
- Recover CloudStack Database Password
- word2vec
- JavaScript的各种对象
- 简单的面包屑数据
- Mac搭建Hexo博客及NexT主题配置优化