首页 > 代码库 > [Leetcode] NO. 171 Excel Sheet Column Number

[Leetcode] NO. 171 Excel Sheet Column Number

[题目] 

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 

[题目解析] 该题目比较简单,可以理解为一个26进制的数用10进制数表示的过程。

    public int titleToNumber(String s) {       if(null == s || s.length() == 0) return -1;       char sarr[] = s.toCharArray();       int base = 26;       int index = sarr.length - 1;       int result = 0;       for(char c : sarr){           int num = c - ‘A‘ + 1;           result += num * Math.pow(base, index);           index--;       }       return result;      }

 

[Leetcode] NO. 171 Excel Sheet Column Number