----------------------------------
乘权相加即可。
AC代码:(从右往左)
public class Solution {
public int titleToNumber(String s) {
int res=0;
for(int i=s.length()-1;i>=0;i--) res+=(s.charAt(i)-'A'+1)*((int)(Math.pow(26,s.length()-i-1)));
return res;
}
}
精简版AC代码:(从左往右)
public class Solution {
public int titleToNumber(String s) {
int res=0;
for(int i=0;i<s.length();i++) res=res*26+s.charAt(i)-'A'+1;
return res;
}
}
题目来源: https://leetcode.com/problems/excel-sheet-column-number/