题目:Excel Sheet Column Number
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 -> 28One:利用字母和数字的关系及Math.pow的double转int
public class Solution {
public int titleToNumber(String s) {
int res = 0;
for(int i = s.length()-1;i>=0;i--){
int temp = s.charAt(i)-'A' + 1;
res = res + temp*(int)Math.pow(26,s.length()-1-i);
}
return res;
}
}
Two:优化
public class Solution {
public int titleToNumber(String s) {
int res = 0;
for(int i = 0;i<s.length();i++){
res*=26;
res+= s.charAt(i)-'A' + 1;
}
return res;
}
}