LeetCode 171. Excel Sheet Column Number

Related to question Excel Sheet Column Title
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

Credits:Special thanks to @ts for adding this problem and creating all test cases.

就是相當于26進制的計算,思路很簡單。

class Solution {
    public int titleToNumber(String s) {
        int result = 0;
        char[] arrays = s.toCharArray();
        for (int i = arrays.length - 1, flag = 0; i >= 0; i--)
            result += (arrays[i] - 64) * Math.pow(26, flag++);
        return result;
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容