版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/majichen95/article/details/82781353
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
样例
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
解题思路:
这就相当于从1开始计数的26进制。多写两个数就发现规律了:
A=26^0 * 1 (26代表进制,1代表A)
AA=26^1 * 1 + 1
AAA=26^2 * 1 + 26^1 * 1 + 1
public class Solution {
/**
* @param s: a string
* @return: return a integer
*/
public int titleToNumber(String s) {
// write your code here
int res = 0;
int flag = 0;
for(int i=s.length()-1 ; i>=0 ; i--,flag++)
res += (s.charAt(i)-'A'+1)*Math.pow(26,flag);
return res;
}
}