171. excel sheet column number

Given a 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
...

Example 1:

1
2
Input: "A"
Output: 1

Example 2:

1
2
Input: "AB"
Output: 28

Example 3:

1
2
Input: "ZY"
Output: 701
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  {
public int titleToNumber(String s) {

int len=s.length();
//保存最终结果
int result=0;
//从个位数开始累加计算
for(int i=len-1;i>=0;i--){
//Math.pow(a,b)是计算a的b次幂,这里底数是26
result+=(s.charAt(i)-'A'+1)*Math.pow(26,len-1-i);
}
return result;
}
}