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
题意:与Excel Sheet Column Title相反,将对应的列标题转换为数字
解决思路:和之前一样,将字母转换为对应数,然后进行26进制加法而已
代码:
public class Solution {
public int titleToNumber(String s) {
int result = 0;
for (int i = 0; i < s.length(); result = result * 26 + (s.charAt(i) - 'A' + 1), i++);
return result;
}
}