leetcode 171. Excel表列序号(Excel Sheet Column Number)

题目描述:

给定一个Excel表格中的列名称,返回其相应的列序号。

例如,

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

示例 1:

    输入: "A"
    输出: 1

示例 2:

    输入: "AB"
    输出: 28

示例 3:

    输入: "ZY"
    输出: 701

解法:

class Solution {
public:
    int titleToNumber(string s) {
        int res = 0;
        vector<int> mp(128, 1);
        for(int ch = 'B'; ch <= 'Z'; ch++){
            mp[ch] = mp[ch-1] + 1;
        }
        for(char ch : s){
            res *= 26;
            res += mp[ch];
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/zhanzq/p/10563428.html
今日推荐