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. 【MySQL】 Cannot load from mysql.proc. The table is probably corrupted

    解决办法 在 mysql 这张表里边.执行sql ALTER TABLE `proc` MODIFY COLUMN `comment` text CHARACTER SET utf8 COLLATE ...

  2. es6 - class的学习

    http://es6.ruanyifeng.com/#docs/class:class Person { constructor{ //构造函数,里边放不被继承的私有属性和方法 this.proper ...

  3. libevent安装方法

    安装FastDFS之前,先安装libevent工具包,记录一下安装过程 1.检查:ls -al /usr/lib | grep libevent 查看是否已安装,如果已安装且版本低于1.3,则先通过: ...

  4. 遍历json数组实现树

    今天小颖在工作中遇到要遍历树得问题了,实现后,怕后期遇到又忘记啦,所以记录下嘻嘻,其实这个和小颖之前写过得一篇文章    json的那些事    中第4点有关json的面试题有些类似. 数组格式: v ...

  5. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十:PS/2模块④ — 普通鼠标

    实验十:PS/2模块④ - 普通鼠标 学习PS/2键盘以后,接下来就要学习 PS/2 鼠标.PS/2鼠标相较PS/2键盘,驱动难度稍微高了一点点,因为FPGA(从机)不仅仅是从PS/2鼠标哪里读取数据 ...

  6. Unity3D Shader描边效果

    Shader "Custom/RimColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whit ...

  7. FPM打包工具使用

    author:headsen chen date: 2019-01-19  14:57:09 个人原创博客,转自请注明出处和作者,否则追究法律责任 1,安装依赖和语言包 yum -y install ...

  8. 【CF932F】Escape Through Leaf 启发式合并set维护凸包

    [CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...

  9. MySQL的btree索引和hash索引的区别 (转)

    Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...

  10. Java Mail 发送邮件(SSL加密方式,TSL加密方式)

    一.一般配置 发送邮件需要用到  mail包 maven 依赖如下: <!-- https://mvnrepository.com/artifact/javax.mail/mail --> ...