思想:
把字符串看成26进制(不是1-27,是0-26)
1.从右向左遍历
2.第i次遍历就是26的(i - 1)次方
代码:
class Solution {
public int titleToNumber(String columnTitle) {
int ans = 0;
for(int i = columnTitle.length() - 1; i >= 0 ;i--)
ans += (columnTitle.charAt(i) - 'A' + 1) * (int)Math.pow(26, columnTitle.length() - i - 1);
return ans;
}
}