168.Excel表列名称

描述

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

26 -> Z
27 -> AA
28 -> AB

示例
输入: 1
输出: “A”

输入: 28
输出: “AB”

输入: 701
输出: “ZY”

class Solution(object):
def convertToTitle(self, n):
"""
:type n: int
:rtype: str
"""
#需要注意26时:26%26为0 也就是0为A 所以使用n-1 A的ASCII码为65
result = ""
while n != 0:
result = chr((n-1)%26+65) + result
n = (n-1)/26
return result

总结一下:
字符与ASCII码的转换:
- 字符转ASCII码 ord(str),如ord(‘A’)为65
- ASCII码转字符 chr(int),如chr(65)为’A’

171题是给字符串求数字,正好与上面的相反

class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
ord(‘A’)为65
"""
res = 0
ll = len(s)
for i in range(ll):
res = 26**(ll-1-i)*(ord(s[i])-64)+res
return res

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. 刷题-力扣-168. Excel表列名称

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

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

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

  5. 力扣168. Excel表列名称

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

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

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

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

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

  8. 【leetcode 简单】第三十九题 Excel表列名称

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

  9. LeetCode168.Excel表列名称

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

随机推荐

  1. [CF1054C]Candies Distribution

    题目:Candies Distribution 传送门:http://codeforces.com/problemset/problem/1054/C 分析: 方法一: 1)类似拓扑排序的做法. 2) ...

  2. 用CSS代码编写简易轮播图

    废话不多说,直接上代码 <!doctype html> <html> <head> <title></title> <meta cha ...

  3. buildroot

    http://buildroot.uclibc.org/downloads/snapshots/buildroot-snapshot.tar.bz2 简介 buildroot是一个Makefiles和 ...

  4. qbzt day5 上午

    动态规划 递推  递归   记忆化搜索 斐波那契数列 1.用其他已经计算好的结果计算自己的结果(递推) 2.用自己的值计算别人的值(考虑对之后的项做出的贡献) cin >> n; f[]= ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_07 Collections工具类_3_Collections集合工具类的方法

    第二个参数传递了一个匿名内部类.结果就出现了下面的代码 源码里面有Compare方法,对比两个参数 要重写比较的方法 对对象进行排序 创建学生类.对学生类进行排序 重写Person的ToString方 ...

  6. windows7搭建xmapp部署wordpress

    前言 为了学习自动化,在网上搜索资料学习了一下在本机安装xmapp,搭建php环境,本机部署wordpress这个开源项目 内容 主要分成以下几步: 准备安装包,快速安装xmapp 根据实际需求,修改 ...

  7. js(javascript)取float型小数点后两位数的方法

    以下我们将为大家介绍 JavaScript 保留两位小数的实现方法:四舍五入以下处理结果会四舍五入: ? 1 2 var num =2.446242342; num = num.toFixed(2); ...

  8. python接口自动化:https请求,取消警告

    实现代码如下: import requests r=requests.get('https://www.baidu.com',verify=False) rr=r.content.decode() p ...

  9. 时间同步,使用oracle自带的ctss

    crsctl check ctss  --observer mode cluvfy comp clocksync   -检查crss为啥没启用 根据不同版本删除ntp的配置和服务 AIX: stops ...

  10. Linq查询语法(1)

    转:http://www.cnblogs.com/ahao214/archive/2013/01/22/2871044.html LINQ的基本格式如下所示:var <变量> = from ...