Leetcode题解 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进制,简单题。
稍微有点难的地方就是这个26进制是从1开始的,而我们通常的机制是从0开始的,仔细思考一下就懂了。

public static int titleToNumber(String s) {
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            result += (s.charAt(i) - 64) * Math.pow(26, s.length() - i - 1);
        }
        return result;
    }

你可能感兴趣的:(LeetCode)