第十二周leetcode题

来源:互联网 发布:mp3统一音量软件 编辑:程序博客网 时间:2024/06/05 20:19

Description:

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 

题目给出的字母组合可以看成是二十七进制数,字母A到Z分别代表1到26,像计算其他进制的数一样计算结果即可。代码如下:

class Solution {
public:
    int titleToNumber(string s) {
        int a=s.length();
        int re=0;
        for(int i=0;i<a;i++)
        {
            re+=pow(26, a-i-1)*(s[i]-'A'+1);
        }
        
        return re;
    }
};

原创粉丝点击