Excel Sheet Column Number
1 min readJun 4, 2019
Question: Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
You may view the full question here.
Approach 1: Assume a numeric system where there are 26 simple digits A-Z and the combination of these digits make numbers. In such a system, each position will have a weight of (26 * position_index). Applying this to build our solution —
//Approach 1
//Runtime: 1ms
//Memory usage: 35MBclass Solution {
public int titleToNumber(String s) {
int len = s.length();
int sum = 0;
for(int i = 0; i<len; i++){
sum = (sum*26) + (s.charAt(i)-'A'+1);
}
return sum;
}
}
Find more posts here.
Cheers & Chao!