当前位置:网站首页>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/
边栏推荐
- Thread data sharing and security -threadlocal
- 在线公网安备案保姆级教程【伸手党福利】
- 【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
- 多元线性回归
- Sort linked list (merge sort)
- Valentine's Day is nothing.
- Leetcode: offer 59 - I. maximum value of sliding window
- File upload and download
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- Appium自动化测试基础 — APPium基本原理
猜你喜欢
![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]

报错:Plug-ins declaring extensions or extension points must set the singleton directive to true

C语言的sem_t变量类型

AfxMessageBox和MessageBox的用法

Leetcode 128 longest continuous sequence (hash set)

idea插件备份表

Jeecgboot output log, how to use @slf4j

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

在线公网安备案保姆级教程【伸手党福利】

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
随机推荐
ECMAScript 6.0
318. 最大单词长度乘积
Data exchange JSON
线程数据共享和安全 -ThreadLocal
Appium自动化测试基础--补充:C/S架构和B/S架构说明
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Home online shopping project
Use of comment keyword in database
Unexpected token o in JSON at position 1 ,JSON解析问题
TEC: Knowledge Graph Embedding with Triple Context
Review column - message queue
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Feature pyramid networks for object detection
LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
File upload and download
Ouc2021 autumn - Software Engineering - end of term (recall version)
Research on target recognition and tracking based on 3D laser point cloud
复习专栏之---消息队列
md5sum操作
How to use hybrid format to output ISO files? isohybrid:command not found