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

Example 2:

Input: "AB"
Output: 28

Example 3:

Input: "ZY"
Output: 701


本质就是将一个26进制的数,转换成10进制的数

class Solution {
    public int titleToNumber(String s) {
        if(s == null && s.length() == 0){
            return 0;
        }
        int len = s.length();
        int sum = 0;
        for(int i=0; i<len; i++){
            int temp = s.charAt(i) - 'A'+ 1;
            sum = sum * 26 + temp;
        }
        return sum;
    }
}

猜你喜欢

转载自www.cnblogs.com/incrediblechangshuo/p/8970255.html
今日推荐