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

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

Cookie&Session

pytorch nn. AdaptiveAvgPool2d(1)

Overview of EtherCAT principle

IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?

Introduction to EtherCAT

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

Test function in pychram

Idea plug-in backup table

实现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)
Valid brackets (force deduction 20)
完全背包问题
Design of serial port receiving data scheme
LeetCode 128最长连续序列(哈希set)
文件上传下载
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
10、Scanner. Next() cannot read spaces /indexof -1
Asgnet paper and code interpretation 2
go实现命令行的工具cli
Blueprism registration, download and install -rpa Chapter 1
Take you through a circuit board, from design to production (dry goods)
Thread data sharing and security -threadlocal
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
How to use hybrid format to output ISO files? isohybrid:command not found
The difference between MFC for static libraries and MFC for shared libraries
You cannot right-click F12 to view the source code solution on the web page
网页不能右键 F12 查看源代码解决方案
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)