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

比较简单,不多说

class Solution {
    public int titleToNumber(String s) {
        if(s == null || s.length() == 0)
            return 0;

        int factor = 1;
        int res = 0;
        for(int i = s.length() - 1; i >= 0; i--) {
            res += (s.charAt(i) - 'A' + 1) * factor;
            factor *= 26;
        }

        return res;
    }
}

results matching ""

    No results matching ""