171-excel-sheet-column-number
Question
https://leetcode.com/problems/excel-sheet-column-number/description/
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
Example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
Thought Process
- Reverse of Mod
- Just multiply and add
- Time complexity O(n)
- Space complexity O(1)
Solution
class Solution {
public int titleToNumber(String s) {
char[] chars = s.toCharArray();
int res = 0;
for (int i = 0; i < chars.length; i++){
res = res * 26 + chars[i] - 'A' + 1;
}
return res;
}
}