码迷,mamicode.com
首页 > 其他好文 > 详细

Excel Sheet Column Number

时间:2015-06-24 23:52:28      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Excel Sheet Column Number

标签:

原文地址:http://www.cnblogs.com/hf-cherish/p/4598879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!