HF_Cherish

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1. Question

将excel表中的列标题(A、B、C...)转换为对应的列数字返回。

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 

2. Solution

是进制转化问题,将用A~Z表示的26进制数转化为10进制数。

    public int titleToNumber( String s ){
        int result=0;
        int pow = (int) Math.pow(26, s.length()-1);
        for( int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            result += pow * (c - 'A' + 1);
            pow /= 26;
        }
        return result;
    }
View Code

 

posted on 2015-06-24 22:52  HF_Cherish  阅读(200)  评论(0编辑  收藏  举报