leetcode 实现-168.Excel表列名称
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表列名称的更多相关文章
- LeetCode 168. Excel表列名称(Excel Sheet Column Title)
168. Excel表列名称 168. Excel Sheet Column Title 题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. LeetCode168. Excel S ...
- Java实现 LeetCode 168 Excel表列名称
168. Excel表列名称 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -&g ...
- 刷题-力扣-168. Excel表列名称
168. Excel表列名称 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/excel-sheet-column-title 著作权 ...
- LeetCode 168. Excel表列名称(Excel Sheet Column Title)
题目描述 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 - ...
- 力扣168. Excel表列名称
原题 1 class Solution: 2 def convertToTitle(self, n: int) -> str: 3 s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ...
- [LeetCode] 168. Excel Sheet Column Title 求Excel表列名称
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- 168 Excel Sheet Column Title Excel表列名称
给定一个正整数,返回它在Excel表中相对应的列名称.示例: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -&g ...
- 【leetcode 简单】第三十九题 Excel表列名称
给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...
- LeetCode168.Excel表列名称
给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...
随机推荐
- YJJ's Salesman
YJJ's Salesman YJJ is a salesman who has traveled through western country. YJJ is always on journey. ...
- 学习wavenet_vocoder之环境配置
WaveNet vocoder位于github的位置,https://github.com/r9y9/wavenet_vocoder 一.配置时的环境 操作系统:win10 python环境工具:An ...
- 使用vue技术应当使用的技术和兼容性选择
假如你的前端框架使用了vue,那你可以大胆地使用以下技术,并忽略其他js和css的兼容性问题,因为 关于vue的兼容性 官方给出了规定 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 ...
- 查看磁盘和文件的使用情况df和du
df, du: disk filesystem, disk usage. df : 查看一级目录的使用情况, df -h du: 则是可以查看目录或者某个文件的占用磁盘空间的情况, du -h: 使用 ...
- fiddler模拟弱网操作
弱网是app测试需要覆盖的一种场景 目录 1.认识弱网 2.fiddler模拟弱网配置 3.弱网下可能发生的问题 1.认识弱网 弱网.2G.3G建议的上下行速率如下,同时还可以控制丢包率的数据 网络 ...
- Android在WindowManagerService和ActivityManagerService中的Token
https://upload-images.jianshu.io/upload_images/5688445-6cf0575bb52ccb45.png 1. ActivityRecord中的token ...
- 用Python处理字幕文件
始 下了部老电影,找到了相关的中英文字幕,奇怪的是,英文字幕能正常现实,中文字幕却不可以,我一度以为是Linux下播放器编码的问题,但是怎么更改设置都无效,而我以前在看其他电影时,中文字幕是正常的.所 ...
- Python笔记(二十九)_模块
模块 在Python中,一个.py文件就是一个模块 if __name__ == '__main__':所有模块都有一个 __name__ 属性,__name__ 的值取决于如何应用模块 run当前文 ...
- Python笔记(二十二)_魔法方法_基本魔法方法
__init__(self[,...]) __init__和__new__组成python的构造器,但__init__更多的是负责初始化操作,相当于一个项目中的配置文件,__new__才是真正的构造函 ...
- [Web 前端] 020 css 定位之绑定定位
绑定定位 少废话,上例子 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...