leedcode Excel 表列序号
自己写的:
class Solution: def titleToNumber(self, columnTitle: str) -> int: # 创建一个字典,将字母映射到它们在字母表中的位置 mydict = dict() for i in range(1, 27): mydict[chr(ord('A') + i - 1)] = i # 计算位数,初始化索引和结果 bit = len(columnTitle) - 1 j = 0 res = 0 # 从高位到低位遍历列标题 while bit >= 0: # 将字母对应的数字乘以 26 的 bit 次方加到结果中 res = res + mydict[columnTitle[j]] * 26 ** bit bit -= 1 j += 1 # 返回最终结果 return res

浙公网安备 33010602011771号