String - 171. Excel Sheet Column Number

2020-09-23 17:11:37 浏览数 (1)

171. Excel Sheet Column Number

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

代码语言:javascript复制
    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

Example 1:

Input: "A" Output: 1

思路:

这一 题是168的逆过程实质就是26进制转换为10进制。

代码:

java:

代码语言:javascript复制
class Solution {
    public int titleToNumber(String s) {
        int res = 0, i = 0;
        while (i < s.length())res = res * 26   s.charAt(i  ) - 'A'   1;        
        return res;
    }
}

0 人点赞