LeetCode 171. Excel Sheet Column

2017-10-09  本文已影响29人  关玮琳linSir

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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读