Excel Sheet Column Number
Problem
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
...
Constraints
1 <= columnTitle.length <= 7
columnTitle
consists only of uppercase English letters.columnTitle
is in the range["A", "FXSHRXW"]
.
Solution
The problem Excel Sheet Column Number
can be solved using the technique from Excel Sheet Column Title except reverse.
Implementation
class Solution
{
public:
int titleToNumber(string columnTitle)
{
int ret = 0;
long mul = 1;
for (int i = columnTitle.length() - 1; i >= 0; i--)
{
ret += mul * (columnTitle[i] - 'A' + 1);
mul *= 26;
}
return ret;
}
};