当前位置:网站首页>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/
边栏推荐
- Valid brackets (force deduction 20)
- leetcode 1482 猜猜看啊,这道题目怎么二分?
- The combination of applet container technology and IOT
- TEC: Knowledge Graph Embedding with Triple Context
- Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- C语言的sem_t变量类型
- Jeecgboot output log, how to use @slf4j
- Review column - message queue
- Thread data sharing and security -threadlocal
猜你喜欢

5、【WebGIS实战】软件操作篇——服务发布及权限管理

不用加减乘除实现加法

LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)

idea插件备份表

用小程序的技术优势发展产业互联网

The combination of applet container technology and IOT

数据库中COMMENT关键字的使用

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C

bootsrap中的栅格系统

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
随机推荐
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
Pathmeasure implements loading animation
Complete knapsack problem
ES6解构语法详解
5、【WebGIS实战】软件操作篇——服务发布及权限管理
Leetcode:829. Sum of continuous integers
4. [WebGIS practice] software operation chapter - data import and processing
Detailed explanation of ES6 deconstruction grammar
Implement pow (x, n) function
Download and installation configuration of cygwin
Valentine's Day is nothing.
Data exchange JSON
Overview of EtherCAT principle
Golang multi graph generation gif
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Thread data sharing and security -threadlocal
在线公网安备案保姆级教程【伸手党福利】
复习专栏之---消息队列
[nine day training] content III of the problem solution of leetcode question brushing Report