LeetCode: Excel Sheet Column Number

LeetCode: Excel Sheet Column Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

/**
* Created by hzhou on 4/23/15. [email protected]
*/
public class ExcelSheetColumnNumber {
public int titleToNumber(String s) {
assert (s != null && !s.isEmpty());
char[] chars = s.toCharArray();
int result = 0;
for (char aChar : chars) {
result = result * 26 + getValue(aChar);
}
return result;
}
private int getValue(char c) {
return c - 'A' + 1;
}
}