171. Excel Sheet Column Number

Related to question Excel Sheet Column Title

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 
题目含义:给定一串字符,求解对应的数字
1     public int titleToNumber(String s) {
2         if(s==null || s.length()== 0) return 0;
3         int sum = 0;
4         for(int i=0; i<s.length(); i++) {
5             sum = 26*sum + s.charAt(i) - 'A' + 1;
6         }
7         return sum;        
8     }

 

posted @ 2017-10-20 19:45  daniel456  阅读(111)  评论(0编辑  收藏  举报