[LeetCode] 168. Excel Sheet Column Title_Easy tag: Math
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
Example 1:
Input: 1
Output: "A"
Example 2:
Input: 28
Output: "AB"
Example 3:
Input: 701
Output: "ZY"
这个题目实际上就想用26进制去将integer转换为string而已.可以看到如果是n%26, 如果是0, 那么就是Z, 否则的话就是chars[n%6], 并且之前的n/26要减一.
Code
1) 基本做法
class Solution:
def convertToTitle(self, n):
chars, rem, ans = 'Z' + string.ascii_uppercase, 1, ""
while n > 0:
rem, n = divmod(n, 26)
ans += chars[n]
if n == 0:
rem -= 1
n = rem
return ans[::-1]
2) 利用chr(ord('A') + n) 去代替chars
class Solution:
def converToTitle(self, n):
ans = ""
while n > 0:
rem, n = divmod(n-1, 26)
ans += chr(ord('A') + n)
n = rem
return ans[::-1]
3) 利用2) 的算法, 但是我们用recursive方式
class Solution:
def converToTitle(self, n):
return "" if n == 0 else self.converToTitle((n-1)/26) + chr(ord('A') + (n-1)%26)
[LeetCode] 168. Excel Sheet Column Title_Easy tag: Math的更多相关文章
- [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number
题目 //像10进制一样进行 转换 只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...
- LeetCode 168. Excel Sheet Column Title (Excel 表格列名称)
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- LeetCode 168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...
- ✡ leetcode 168. Excel Sheet Column Title 26进制数字 --------- java
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- Java for LeetCode 168 Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- Java [Leetcode 168]Excel Sheet Column Title
题目描述: Given a positive integer, return its corresponding column title as appear in an Excel sheet. F ...
- LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)
翻译 给定一个正整数,返回它作为出如今Excel表中的正确列向标题. 比如: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 - ...
- Leetcode 168 Excel Sheet Column Title 进制数转化
题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- class Solution { public: string ...
随机推荐
- spring cache 详解
Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...
- windows 电脑配置信息检测
内存条 DDR4 DDR4相比DDR3最大的区别有: 1)处理器:每次内存升级换代时,必须支持的就是处理器.Haswell-E平台的内存同IVB-E/SNB-E一样为四通道设计,DDR4内存频率原生支 ...
- 查看JVM使用的默认的垃圾收集器
一.查看步骤 cmd执行命令: java -XX:+PrintCommandLineFlags -version 输出如下(举例): 针对上述的-XX:UseParallelGC,这边我们引用< ...
- Mysql On Mac OS: Remove & Install
If you downloaded and installed from .dmg package already, and mightbe sometime it sucks because of ...
- matlab中 数据保留有效位数
可以使用round函数 ,这函数原本功能是四舍五入 比如: >> A = 0.0326465;>> B = round(A*1000)/1000 B = 0.0330
- Bulk Convert DOC to DOCX
原文链接 :http://blogs.msdn.com/b/ericwhite/archive/2008/09/19/bulk-convert-doc-to-docx.aspx 帮助文档:http:/ ...
- Microsoft Office Enterprise 2007 在安装过程中出错的解决方法
今天笔者在使用PowerPoint 2007打开一个ppt的内嵌的excel表格时报如下错误: 无法找到 服务器应用程序.源文件.和项目,或返回的未知错误.请重新安装服务程序 然后就先把ppt文件发给 ...
- 【CF840E】In a Trap 分块
[CF840E]In a Trap 题意:一棵n个点的树,第i个点权值为ai,q次询问,每次给定u,v(u是v的祖先),求对于所有在u-v上的点i,$a_i\ \mathrm{xor}\ dis(i, ...
- python 测试框架之---testtools
在tempest框架中,使用的是testtools为基础框架来运行接口自动化 一.初识 testools是属于python中诸多自动化框架中的一个,官方文档如下: http://testtools.r ...
- A simple guide to 9-patch for Android UI
extends:http://radleymarx.com/blog/simple-guide-to-9-patch/ While I was working on my first Android ...