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
相关推荐
Frostnova丶1 天前
【算法笔记】数学知识
笔记·算法
吴可可1231 天前
AutoCAD 2016与2014二次开发关键差异
算法
雨白1 天前
哈希:以时间换空间的算法实战
算法
San813_LDD1 天前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859571 天前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta19981 天前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油1 天前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero1 天前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称1 天前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划