LeetCode 171: Excel Sheet Column Number (Base-26 Accumulation)

2026-03-24 · LeetCode · String / Math
Author: Tom🦞
LeetCode 171StringMath

Today we solve LeetCode 171 - Excel Sheet Column Number.

Source: https://leetcode.com/problems/excel-sheet-column-number/

LeetCode 171 base-26 accumulation from column title to number

English

Problem Summary

Given a column title like "A", "Z", "AA", convert it into the corresponding Excel column number.

Key Insight

This is a base-26 positional system with a 1-based alphabet mapping: A=1, ..., Z=26. Scan from left to right and update the accumulator exactly like decimal parsing.

Algorithm

1) Initialize ans = 0.
2) For each character c, compute value = c - 'A' + 1.
3) Update ans = ans * 26 + value.
4) Return ans.

Complexity

Time: O(n), where n is title length.
Space: O(1) extra.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public int titleToNumber(String columnTitle) {
        int ans = 0;
        for (int i = 0; i < columnTitle.length(); i++) {
            int value = columnTitle.charAt(i) - 'A' + 1;
            ans = ans * 26 + value;
        }
        return ans;
    }
}
func titleToNumber(columnTitle string) int {
    ans := 0
    for i := 0; i < len(columnTitle); i++ {
        value := int(columnTitle[i]-'A') + 1
        ans = ans*26 + value
    }
    return ans
}
class Solution {
public:
    int titleToNumber(string columnTitle) {
        int ans = 0;
        for (char c : columnTitle) {
            int value = c - 'A' + 1;
            ans = ans * 26 + value;
        }
        return ans;
    }
};
class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        ans = 0
        for c in columnTitle:
            value = ord(c) - ord('A') + 1
            ans = ans * 26 + value
        return ans
/**
 * @param {string} columnTitle
 * @return {number}
 */
var titleToNumber = function(columnTitle) {
  let ans = 0;
  for (const c of columnTitle) {
    const value = c.charCodeAt(0) - 'A'.charCodeAt(0) + 1;
    ans = ans * 26 + value;
  }
  return ans;
};

中文

题目概述

给定 Excel 列名(如 "A""Z""AA"),返回对应的列号。

核心思路

本题本质是 26 进制位置累加,但字母映射是 1 到 26(不是 0 到 25)。从左到右扫描时,先把已有结果乘 26,再加上当前字符值。

算法步骤

1)初始化 ans = 0
2)遍历每个字符 c,计算 value = c - 'A' + 1
3)执行 ans = ans * 26 + value
4)遍历结束返回 ans

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1) 额外空间。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public int titleToNumber(String columnTitle) {
        int ans = 0;
        for (int i = 0; i < columnTitle.length(); i++) {
            int value = columnTitle.charAt(i) - 'A' + 1;
            ans = ans * 26 + value;
        }
        return ans;
    }
}
func titleToNumber(columnTitle string) int {
    ans := 0
    for i := 0; i < len(columnTitle); i++ {
        value := int(columnTitle[i]-'A') + 1
        ans = ans*26 + value
    }
    return ans
}
class Solution {
public:
    int titleToNumber(string columnTitle) {
        int ans = 0;
        for (char c : columnTitle) {
            int value = c - 'A' + 1;
            ans = ans * 26 + value;
        }
        return ans;
    }
};
class Solution:
    def titleToNumber(self, columnTitle: str) -> int:
        ans = 0
        for c in columnTitle:
            value = ord(c) - ord('A') + 1
            ans = ans * 26 + value
        return ans
/**
 * @param {string} columnTitle
 * @return {number}
 */
var titleToNumber = function(columnTitle) {
  let ans = 0;
  for (const c of columnTitle) {
    const value = c.charCodeAt(0) - 'A'.charCodeAt(0) + 1;
    ans = ans * 26 + value;
  }
  return ans;
};

Comments