当前位置:网站首页>171. Excel 表列序号
171. Excel 表列序号
2022-07-01 03:23:00 【Sun_Sky_Sea】
171. Excel 表列序号
原始题目链接:https://leetcode.cn/problems/excel-sheet-column-number/
给你一个字符串 columnTitle ,表示 Excel 表格中的列名称。返回 该列名称对应的列序号 。
例如:
A -> 1
B -> 2
C -> 3
…
Z -> 26
AA -> 27
AB -> 28
…
示例 1:
输入: columnTitle = “A”
输出: 1
示例 2:
输入: columnTitle = “AB”
输出: 28
示例 3:
输入: columnTitle = “ZY”
输出: 701
提示:
1 <= columnTitle.length <= 7
columnTitle 仅由大写英文组成
columnTitle 在范围 [“A”, “FXSHRXW”] 内
解题思路:
这道题就是将26进制的字母转换成对应的十进制的数字,先将字母转换对应的26进制的整数,再将这些整数按照从低位到高位乘以对应的底数,转换成10进制数,这里要注意columnTitle最右边的字母是最低位,需要倒叙遍历。
代码实现:
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
ans = 0
# 26进制的底数
mul = 1
# 倒叙遍历columnTitle,因为最小编号在最右边
for i in range(len(columnTitle) - 1, -1, -1):
# 为了转换成十进制的数,先计算ASCII的值
cur = ord(columnTitle[i]) - ord('A') + 1
ans += cur * mul
mul *= 26
return ans
参考文献:
https://leetcode.cn/problems/excel-sheet-column-number/solution/excelbiao-lie-xu-hao-by-leetcode-solutio-r29l/
边栏推荐
猜你喜欢

Bilinear upsampling and f.upsample in pytorch_ bilinear

The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list

Valentine's Day is nothing.

Data exchange JSON
![[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st](/img/9f/187ca83be1b88630a6c6fbfb0620ed.png)
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st

pytorch训练深度学习网络设置cuda指定的GPU可见

Use of comment keyword in database

Explain spark operation mode in detail (local+standalone+yarn)

Sort linked list (merge sort)

Filter
随机推荐
打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
Pytorch training deep learning network settings CUDA specified GPU visible
小程序容器技术与物联网IoT的结合点
Use of comment keyword in database
Cookie&Session
Promise中finally的用法
Download and installation configuration of cygwin
318. 最大单词长度乘积
串口接收数据方案设计
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
Pyramid scene parsing network [pspnet] thesis reading
RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs
Include() of array
【快捷键】
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Complete knapsack problem
Data exchange JSON
二叉树神级遍历:Morris遍历