原题链接:https://leetcode.com/problems/excel-sheet-column-number/description/
这题没啥技术含量,直接就是实现:
/**
* Created by clearbug on 2018/2/26.
*/
public class Solution {
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.titleToNumber("A"));
System.out.println(s.titleToNumber("B"));
System.out.println(s.titleToNumber("Z"));
System.out.println(s.titleToNumber("AA"));
System.out.println(s.titleToNumber("AB"));
System.out.println(s.titleToNumber("AZ"));
}
public int titleToNumber(String s) {
int res = 0;
for (int i = 0; i < s.length(); i++) {
res += titleToNumberHelper(s.charAt(i)) * Math.pow(26, s.length() - 1 - i);
}
return res;
}
private int titleToNumberHelper(char c) {
return c - 'A' + 1;
}
}