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 -> ...
随机推荐
- [CSP-S模拟测试]:小P的2048(模拟)
题目描述 最近,小$P$迷上了一款叫做$2048$的游戏.这块游戏在一个$n\times n$的棋盘中进行,棋盘的每个格子中可能有一个形如$2^k(k\in N^*)$的数,也可能是空的.游戏规则介绍 ...
- onchange and oninput
https://www.w3schools.com/jsref/event_oninput.asp Supported HTML tags: <input type="color&qu ...
- Understanding the Module Pattern in JavaScript
Understanding the Module Pattern in JavaScript Of all the design patterns you are likely to encounte ...
- 顶级域名、一级域名、二级域名与IP
转自:https://blog.csdn.net/qq_38071429/article/details/80339091 域名:可分三级,一级域名,二级域名,三级域名.是由一串字符+域名后缀组成,我 ...
- 背包&数位dp(8.7)
背包 0/1背包 设dp[i][j]为前i个物品选了j体积的物品的最大价值/方案数 dp[i][j]=max(dp[i-1][j-w[i]]+v[i],dp[i-1][j])(最大价值) dp[i][ ...
- 使用wxpy模块了解微信好友
网上看了一篇python文章,内容简单有趣,正好可以练习一下.原文连接:http://mp.weixin.qq.com/s/oI2pH8uvq4kwYqc4kLMjuA 一.环境:Windows+py ...
- R 时间戳转化
转 http://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r You want the (standard) PO ...
- 文件格式-CVS:CVS
ylbtech-文件格式-CVS:CVS 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文 ...
- java日期处理的一些例子使用...
一.计算成为会员多少天 需求:根据会员的创建日期createTime,计算成为会员多少天. 计算:当前日期 - 创建日期,转化为天数,即为成为会员多少天. 代码: public static void ...
- PANIC: Missing emulator engine program for ‘x86’ CPU.
参考链接:https://zhidao.baidu.com/question/652153765084187325.html 解决方案:看图最上面路径,进入你的文件夹下,把红文件夹 ( 1 ) 中的所 ...