leetcode Excel Sheet Column Number

来源:互联网 发布:淘宝卖家如何拉客户端 编辑:程序博客网 时间:2024/05/29 16:09

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

这实际是26进制,跟问你12345是多少一样的。

12345=5*1+4*10+3*100+2*1000+1*10000

ABC=1*C+26*B+26*26*A

public class Solution {

    static HashMap<Character,Integer>map=new HashMap<Character,Integer>();
static {
char temp='A';
for(int i=0;i<26;i++){
map.put((char) (temp+i), i+1);
}

}
    public static int titleToNumber(String s) {
        int len=s.length();
int tmep=26;
int ret=0;
for(int i=len-1;i>=0;i--){
ret+=map.get(s.charAt(i))*Math.pow(tmep, len-1-i);
}
return ret;
    }
}
0 0