当前位置:网站首页>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/
边栏推荐
猜你喜欢

Take you through a circuit board, from design to production (dry goods)

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

复习专栏之---消息队列

静态库使用MFC和共享库使用MFC的区别

Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)

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

Detailed list of errors related to twincat3 ads of Beifu

Appium fundamentals of automated testing - basic principles of appium

Listener listener

FCN全卷积网络理解及代码实现(来自pytorch官方实现)
随机推荐
静态库使用MFC和共享库使用MFC的区别
5、【WebGIS实战】软件操作篇——服务发布及权限管理
ES6解构语法详解
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Gorilla/mux framework (RK boot): RPC error code design
GCC usage, makefile summary
Jeecgboot output log, how to use @slf4j
md5sum操作
Idea plug-in backup table
Error: plug ins declaring extensions or extension points must set the singleton directive to true
实现pow(x,n)函数
Unexpected token o in JSON at position 1 ,JSON解析问题
Bilinear upsampling and f.upsample in pytorch_ bilinear
torch.histc
Depth first traversal of C implementation Diagram -- non recursive code
Include() of array
【伸手党福利】开发人员重装系统顺序
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
[reach out to Party welfare] developer reload system sequence
Leetcode:剑指 Offer 59 - I. 滑动窗口的最大值