String - 171. Excel Sheet Column Number

6

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 
    ...

Example 1:

Input: "A"
Output: 1

思路:

这一 题是168的逆过程实质就是26进制转换为10进制。

代码:

java:

class Solution {
    public int titleToNumber(String s) {
        int res = 0, i = 0;
        while (i < s.length())res = res * 26 + s.charAt(i++) - 'A' + 1;        
        return res;
    }
}