当前位置:网站首页>168. Excel表列名称
168. Excel表列名称
2022-07-01 03:23:00 【Sun_Sky_Sea】
168. Excel表列名称
原始题目链接:https://leetcode.cn/problems/excel-sheet-column-title/
给你一个整数 columnNumber ,返回它在 Excel 表中相对应的列名称。
例如:
A -> 1
B -> 2
C -> 3
…
Z -> 26
AA -> 27
AB -> 28
…
示例 1:
输入:columnNumber = 1
输出:“A”
示例 2:
输入:columnNumber = 28
输出:“AB”
示例 3:
输入:columnNumber = 701
输出:“ZY”
示例 4:
输入:columnNumber = 2147483647
输出:“FXSHRXW”
解题思路:
通过计算余数和商来从低位到高位算出对应的字母,这里26进制的计算是0到25这个26个数,但A是从1开始的,所以在计算余数和商的时候,为了能够和26进制保持一样,所以先减掉1,再计算。
代码实现:
class Solution:
def convertToTitle(self, columnNumber: int) -> str:
ans = []
while columnNumber > 0:
# 因为是26进制:0~25,但A是从1开始的,所以字母相对于正常的26进制的数字
# 都多加了一个1,所以在使用26进制计算的时候,先减掉1个1
columnNumber -= 1
# 计算余数,即最右边的字母
cur = columnNumber % 26
# 转换字母
cur_ans = chr(cur + ord('A'))
ans.insert(0, cur_ans)
# 求商,计算下一个字母
columnNumber //= 26
return ''.join(ans)
参考文献:
https://leetcode.cn/problems/excel-sheet-column-title/solution/excelbiao-lie-ming-cheng-by-leetcode-sol-hgj4/
边栏推荐
- JS daily development tips (continuous update)
- 静态库使用MFC和共享库使用MFC的区别
- Pathmeasure implements loading animation
- [nine day training] content III of the problem solution of leetcode question brushing Report
- Complete knapsack problem
- [small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
- BluePrism注册下载并安装-RPA第一章
- Valentine's Day is nothing.
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- jeecgboot输出日志,@Slf4j的使用方法
猜你喜欢

BluePrism注册下载并安装-RPA第一章
![Online public network security case nanny level tutorial [reaching out for Party welfare]](/img/66/d9c848a7888e547b7cb28d84aabc24.png)
Online public network security case nanny level tutorial [reaching out for Party welfare]

4、【WebGIS实战】软件操作篇——数据导入及处理

pytorch nn. AdaptiveAvgPool2d(1)

实现pow(x,n)函数

Leetcode 31 next spread, leetcode 64 minimum path sum, leetcode 62 different paths, leetcode 78 subset, leetcode 33 search rotation sort array (modify dichotomy)

Use of comment keyword in database

C语言的sem_t变量类型

Unexpected token o in JSON at position 1 ,JSON解析问题

后台系统右边内容如何出现滚动条和解决双滚动条的问题
随机推荐
线程数据共享和安全 -ThreadLocal
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Finally in promise
md5sum操作
pytorch nn. AdaptiveAvgPool2d(1)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
Implement pow (x, n) function
jeecgboot输出日志,@Slf4j的使用方法
完全背包问题
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值
How do I use Google Chrome 11's Upload Folder feature in my own code?
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
Leetcode: offer 59 - I. maximum value of sliding window
[reach out to Party welfare] developer reload system sequence
Idea plug-in backup table
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
Asgnet paper and code interpretation 2
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
过滤器 Filter