当前位置:网站首页>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/
边栏推荐
- 详解Spark运行模式(local+standalone+yarn)
- Filter
- Database DDL (data definition language) knowledge points
- Appium fundamentals of automated testing - basic principles of appium
- The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
- Blueprism registration, download and install -rpa Chapter 1
- Go tool cli for command line implementation
- The shell script uses two bars to receive external parameters
- You cannot right-click F12 to view the source code solution on the web page
- LeetCode 128最长连续序列(哈希set)
猜你喜欢

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 144二叉树的前序遍历、LeetCode 114二叉树展开为链表

Cygwin的下载和安装配置

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

E15 solution for cx5120 controlling Huichuan is620n servo error

用小程序的技术优势发展产业互联网
![Online public network security case nanny level tutorial [reaching out for Party welfare]](/img/66/d9c848a7888e547b7cb28d84aabc24.png)
Online public network security case nanny level tutorial [reaching out for Party welfare]

实现pow(x,n)函数

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

谷粒学院微信扫码登录过程记录以及bug解决
随机推荐
You cannot right-click F12 to view the source code solution on the web page
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 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
IPv4 and IPv6, LAN and WAN, gateway, public IP and private IP, IP address, subnet mask, network segment, network number, host number, network address, host address, and IP segment / number - what does
[nine day training] content III of the problem solution of leetcode question brushing Report
【伸手党福利】JSONObject转String保留空字段
Golang multi graph generation gif
Leetcode 1482 guess, how about this question?
[daily training] 1175 Prime permutation
Edlines: a real time line segment detector with a false detection control
Pyramid Scene Parsing Network【PSPNet】论文阅读
文件上传下载
在 C 中声明函数之前调用函数会发生什么?
Use of comment keyword in database
访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
报错:Plug-ins declaring extensions or extension points must set the singleton directive to true
TEC: Knowledge Graph Embedding with Triple Context
leetcode 1482 猜猜看啊,这道题目怎么二分?
Split(), split(), slice(), can't you tell?