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

难度:easy

题目:给定Excel 表单里的一列名,返回其整数表示。

思路:26进制

Runtime: 1 ms, faster than 100.00% of Java online submissions for Excel Sheet Column Number.
Memory Usage: 34.5 MB, less than 86.88% of Java online submissions for Excel Sheet Column Number.

class Solution {
    public int titleToNumber(String s) {
        int result = 0, scale = 1;
        for (int i = s.length() - 1; i >= 0; i--) {
            result += (s.charAt(i) - 'A' + 1) * scale;
            scale *= 26;
        }
        
        return result;
    }
}

linm
1 声望4 粉丝

〜〜〜学习始于模仿,成长得于总结〜〜〜