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
思路:这个实际就是一个26进制数的转化问题。
代码如下(已通过leetcode)
public class Solution {
public int titleToNumber(String s) {
if(s==null) return 0;
int length=s.length();
String s2="";
for(int i=length-1;i>=0;i--) {
s2+=s.charAt(i);
}
int sum=s2.charAt(0)-'A'+1;
for(int i=1;i<length;i++) {
sum+=jiechen(i)*(s2.charAt(i)-'A'+1);
}
return sum;
}
public int jiechen(int n) {
int ans=1;
for(int j=0;j<n;j++)
ans=ans*26;
return ans;
}
}