171. Excel Sheet Column Number(简单数学题)

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 
    ...

Example 1:

Input: "A"
Output: 1

Example 2:

Input: "AB"
Output: 28

Example 3:

Input: "ZY"
Output: 701


 1 class Solution {
 2     public int titleToNumber(String s) {
 3         int result = 0;
 4         int i = 0; 
 5         while(i < s.length()){
 6             result = result * 26 + (s.charAt(i) - 'A' + 1);
 7             i++;
 8         }
 9         return result;
10     }
11 }

 

posted @ 2018-06-11 14:41  乐乐章  阅读(217)  评论(0编辑  收藏  举报