字母轉換爲數字 Excel Sheet Column Number

問題:app

Related to question Excel Sheet Column Titleexcel

Given a column title as appear in an Excel sheet, return its corresponding column number.code

For example:leetcode

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

解決:get

① 能夠當作26進制數的運算來理解。it

public class Solution {//2ms
    public int titleToNumber(String s) {
        int res = 0;
        for (int i = 0;i < s.length();i ++ ) {
            int tmp = ((s.charAt(i) - 'A') + 1);
            res = res * 26 + tmp;
        }
        return res;
    }
}io

相關文章
相關標籤/搜索