题目:
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
解决方案:
public class Solution {
public int titleToNumber(String s) {
int n = s.length();
int res = 0;
for(int i = 0; i < s.length(); i++){
res += ((int)(s.charAt(i)-'A'+1)) * Math.pow(26,n-1);
n--;
}
return res;
}
}
总结:
这道题目与上一篇文章是类似的,在那道题目中衍生出来的,还是26进制与10进制的转换问题,自己在这里犯二了,忘记Java中取字符串的第i个字符的方法了,这里是用charAt方法。还有是pow的用法。
浙公网安备 33010602011771号