题目:
给定Excel表中出现的列标题,返回对应的列号。
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 ... Input: "A" Output: 1 Input: "AB" Output: 28 Input: "ZY" Output: 701
思路:
根据输入输出可以得到,类似于一个26进制
A=1
AB = 1*26+1
ZY = 27*26 + 26
所以依次从最低位开始累加即可
代码:
class Solution {
public int titleToNumber(String s) {
int re=0;
int base = 1;
for(int i=s.length()-1;i>=0;i--) {
char t = s.charAt(i);
int n = t - 'A' + 1;
re = re + n*base;
System.out.println();
base = base*26;
}
return re;
}
}