美文网首页
LeetCode 171. Excel Sheet Column

LeetCode 171. Excel Sheet Column

作者: 关玮琳linSir | 来源:发表于2017-10-09 10:54 被阅读29次

    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;
        }
    }
    

    相关文章

      网友评论

          本文标题:LeetCode 171. Excel Sheet Column

          本文链接:https://www.haomeiwen.com/subject/bbvuyxtx.html