171. Excel Sheet Column Number
Related to question Excel Sheet Column Title
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 AB -> 28
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
题意:
按照Excel中表的列的名字的命名规范,把字符串转化为数字
算法思路:
这个题其实要表达的意思就是要把一个26进制的数转化为一个十进制的数
所以按照10进制的那种算法,移位即乘以进率就行
代码:
package easy;
/*
@author wchstrife
@version 2017年8月20日下午3:03:12
*/
public class ExcelSheetColumnNumber {
public static int titleToNumber(String s) {
int sum = 0;
int tmp = 0;
for(int i=0; i<s.length(); i++){
tmp = s.charAt(i) - 'A' + 1;
sum = (sum * 26) + tmp;
}
return sum;
}
public static void main(String[] args){
System.out.println(titleToNumber("AA"));
}
}