LeetCode | 171.Excel表列序号

这道题涉及到字符串和进制转换,首先我们先创建一个A-Z到1-26的map映射,方便我们后续遍历字符串转换,然后对字符串从后往前遍历,依次加上对应权重,注意越往前的权重越大,要记得对应乘上26的对应方数

python 复制代码
class Solution(object):
    def titleToNumber(self, columnTitle):
        """
        :type columnTitle: str
        :rtype: int
        """
        ans = 0
        base = 1
        index = len(columnTitle) - 1
        # 使用字典推导式创建映射字典
        mapping = {chr(i): i - 64 for i in range(65, 91)}
        while index >= 0:
            ans += mapping[columnTitle[index]] * base
            base *= 26
            index -= 1
        return ans
相关推荐
罗西的思考5 小时前
【Agentic RL】Dressage Staleness 控制深度分析
人工智能·算法·机器学习
quaer5 小时前
弗洛伊德算法--判圈法寻找环的入口O(n) O(1)
数据结构·算法
文丑颜不良啊6 小时前
LeetCode51~75题目解析
leetcode
高晶6 小时前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
ysbfsy6 小时前
大数计算思路
算法
高晶6 小时前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
学电子她就能回来吗7 小时前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)
笔记·算法·leetcode
用户老李7 小时前
反向传播计算梯度举例
算法
niyongsheng7 小时前
多智能体连续时间内路径规划:AA-SIPP(m) 的 Rust 实现与算法
算法·rust·机器人
302wanger8 小时前
DeepSeek-V4.1-Flash-1017 免费用到1024,速来
算法