题目内容
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进制的数字转换问题就可以了。
public class Solution {
public int titleToNumber(String s) {
int num=0;
for(int i=s.length()-1;i>=0;i--)
{
int temp=(s.charAt(i)-'A'+1);
for(int j=0;j<s.length()-1-i;j++)
{
temp*=26;
}
num+=temp;
}
return num;
}
}