171. Excel Sheet Column Number

Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:app

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

Example 1:less

Input: "A"
Output: 1

Example 2:code

Input: "AB"
Output: 28

Example 3:it

Input: "ZY"
Output: 701

難度:easyio

題目:給定Excel 表單裏的一列名,返回其整數表示。ast

思路:26進制class

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;
    }
}
相關文章
相關標籤/搜索