[LeetCode] Excel Sheet Column Number

171. Excel Sheet Column Number

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
...
1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int titleToNumber(string columnTitle) {
int res = 0;
for(auto ch : columnTitle) {
res = res * 26 + (ch - 'A' + 1);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/22/PS/LeetCode/excel-sheet-column-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.