cKK

............当你觉得自己很辛苦,说明你正在走上坡路.............坚持做自己懒得做但是正确的事情,你就能得到别人想得到却得不到的东西............

导航

Excel Sheet Column Number

Posted on 2015-06-03 21:45  cKK  阅读(188)  评论(0编辑  收藏  举报

主意利用asc码

/*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 */
public class ExcelSheetColumnNumber1 {
    public static void main(String[] args)
    {
        System.out.println(titleToNumber("AB"));
    }



     public static int titleToNumber(String s) {
         
         int sum=0;
         char[] ch=s.toCharArray();
        for(int i=s.length()-1;i>=0;i--)        
            sum+=(ch[i]-64)*Math.pow(26,(s.length()-i-1) );    
        return sum;
        }





}