Leetcode 171 Excel Sheet Column Number

2018-01-12 14:48:34 浏览数 (1)

Related to question Excel Sheet Column Title

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 

字符串转数字

类似于进制转换,可以理解为26进制转10进制

代码语言:javascript复制
class Solution {
public:
    int titleToNumber(string s) {
        int res = 0 , base = 1;
        for(int i = s.size()-1; i >= 0; i--)
        {
            res  = base*(s[i] 1-'A');
            base*=26;
        }
        return res;
    }
};

0 人点赞