171. Excel 表列序号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.math.*;
class Solution {
public int titleToNumber(String columnTitle) {
if(columnTitle == null){
return 0;
}

int n = columnTitle.length();
int res = 0;

// 大A的为65
for(int i = 0; i < columnTitle.length(); i++){
char x = columnTitle.charAt(i);
int k = Integer.valueOf(x) - Integer.valueOf('A') + 1 ;
res += Math.pow(26, n - i - 1) * k;
}

return res;
}
}