当前位置:网站首页>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/
边栏推荐
- Appium fundamentals of automated testing - basic principles of appium
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- 在线公网安备案保姆级教程【伸手党福利】
- 4、【WebGIS实战】软件操作篇——数据导入及处理
- Depth first traversal of C implementation Diagram -- non recursive code
- 静态库使用MFC和共享库使用MFC的区别
- Feature pyramid networks for object detection
- 网页不能右键 F12 查看源代码解决方案
- 快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
- [小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
猜你喜欢

Test function in pychram

Addition without addition, subtraction, multiplication and division

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

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

完全背包问题

深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)

不用加减乘除实现加法
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

Appium自动化测试基础 — APPium基本原理
![[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation](/img/b3/887d3fb64acbf3702814d32e2e6414.png)
[small sample segmentation] interpretation of the paper: prior guided feature enrichment network for fee shot segmentation
随机推荐
4、【WebGIS实战】软件操作篇——数据导入及处理
线程数据共享和安全 -ThreadLocal
[reach out to Party welfare] developer reload system sequence
Md5sum operation
数据交换 JSON
Test function in pychram
BluePrism注册下载并安装-RPA第一章
Depth first traversal of C implementation Diagram -- non recursive code
How to use hybrid format to output ISO files? isohybrid:command not found
串口接收数据方案设计
GCC usage, makefile summary
在 C 中声明函数之前调用函数会发生什么?
File upload and download
Feature Pyramid Networks for Object Detection论文理解
AfxMessageBox和MessageBox的用法
监听器 Listener
二叉树神级遍历:Morris遍历
Leetcode: offer 59 - I. maximum value of sliding window
Thread data sharing and security -threadlocal
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)