Excel Sheet Column Number Problem


Description

LeetCode Problem 171.

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

For example:

1
2
3
4
5
6
7
8
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

Example 1:

1
2
Input: columnTitle = "A"
Output: 1

Example 2:

1
2
Input: columnTitle = "AB"
Output: 28

Example 3:

1
2
Input: columnTitle = "ZY"
Output: 701

Example 4:

1
2
Input: columnTitle = "FXSHRXW"
Output: 2147483647

Constraints:

  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range [“A”, “FXSHRXW”].


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
    int titleToNumber(string s) {
        int n = s.size();
        if (n == 0)
            return 0;
        int ans = 0;
        for (int i = 0; i < n; i ++) {
            ans *= 26;
            ans += s[i] - 'A' + 1;
        }
        return ans;
    }
};




Related Posts

Largest Number Problem

LeetCode 179. Given a list of non-negative integers nums, arrange...

Excel Sheet Column Title Problem

LeetCode 168. Given an integer columnNumber, return its corresponding column...

Excel Sheet Column Number Problem

LeetCode 171. Given a string columnTitle that represents the column...

Valid Number Problem

LeetCode 65. Given a string s, return true if s...

Text Justification Problem

LeetCode 68. Given an array of strings words and a...

Valid Number Problem

LeetCode 67. A robot is located at the top-left corner...