Fork me on GitHub

LeetCode之171. Excel Sheet Column Number

----------------------------------

 

乘权相加即可。

 

AC代码:(从右往左)

public class Solution {
    public int titleToNumber(String s) {
        int res=0;
        for(int i=s.length()-1;i>=0;i--) res+=(s.charAt(i)-'A'+1)*((int)(Math.pow(26,s.length()-i-1)));
        return res;
    }
}

 

精简版AC代码:(从左往右)

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

 

题目来源: https://leetcode.com/problems/excel-sheet-column-number/

posted @ 2016-10-25 22:21  CC11001100  阅读(234)  评论(0编辑  收藏  举报