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的更多相关文章

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

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

  2. leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number

    题目 //像10进制一样进行 转换   只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...

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

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

  4. LeetCode 168. Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. -> A ...

  5. ✡ 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 ...

  6. 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 ...

  7. Java [Leetcode 168]Excel Sheet Column Title

    题目描述: Given a positive integer, return its corresponding column title as appear in an Excel sheet. F ...

  8. LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)

    翻译 给定一个正整数,返回它作为出如今Excel表中的正确列向标题. 比如: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 - ...

  9. Leetcode 168 Excel Sheet Column Title 进制数转化

    题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- class Solution { public: string ...

随机推荐

  1. c++ 类内static成员初始化

    类内部的static成员,除了为const static 且为整数类型(int char bool)可在类内部初始化. 其他的都建议在对应的cpp文件中进行初始化. test.h #ifndef TE ...

  2. vue笔记 - 组件间通信 之 中央事件总线bus

    中央事件总线 - 就是一个名字可以叫做bus的vue空实例,里边没有任何内容: var bus = new Vue(); 人如其名,她就像一个公交车一样,来回输送人,将a站点的A输送到b站点,再将b站 ...

  3. 流程图 --- BPMN规范简介

    BPMN 目前 是2.0规范 http://www.bpmn.org/   BPMN Quick Guide http://blog.csdn.net/flygoa/article/details/5 ...

  4. Calling a Java Method from Native Code

    http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html Calling Java Method ...

  5. html5里面的延迟加载属性

    html5中给script标签引入了 async 和 defer 属性. 原理:带有async属性的script标签,会在浏览器解析时立即下载脚本同时不阻塞后续的document渲染和script加载 ...

  6. tcp连接的状态变迁以及如何调整tcp连接中处于time_wait的时间

    一.状态变迁图 二.time_wait状态 针对time_wait和close_wait有个简单的描述帮助理解: Due to the way TCP/IP works, connections ca ...

  7. Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1

    退出当前环境: logout (再重新登录进去) yum install python-devel  -yyum install libevent-devel  -y 把环境更新下yum instal ...

  8. windows下java开发资料汇总

    开发环境搭建:   (1) java开发环境配置    (2) maven环境快速搭建        项目部署:   (1) Eclipse中项目部署方法   (2) 使用Eclipse构建Maven ...

  9. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  10. NuGet 安装EntityFramework5 历程

    第一步:VS2012中 (据说VS2010还得安装一下NuGet)工具->库程序包管理器->程序包管理器控制台,打开控制台 Install-Package EntityFramework ...