Home Leetcode 171 - Excel Sheet Column Number
Post
Cancel

Leetcode 171 - Excel Sheet Column Number

Link to problem

Description

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3

Z -> 26
AA -> 27
AB -> 28

Solution

Observe :)

Implementation

1
2
3
4
5
6
7
8
impl Solution {
    pub fn title_to_number(column_title: String) -> i32 {
        column_title
            .chars()
            .map(|c| c as i32 - 'A' as i32 + 1)
            .fold(0, |acc, c| acc * 26 + c)
    }
}
This post is licensed under CC BY 4.0 by the author.