171. Excel Sheet Column Number

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 

从26进制转换到10进制,

class Solution {
public:
    int titleToNumber(string s) {
        int n = s.length();
        int t = 1, x, sum = 0;
        for (int i = n - 1; i >= 0; --i) {
            x = (s[i] - 'A') + 1;
            x *= t;
            sum += x;
            t *= 26;
        }
        return sum;
    }
};

 

posted on 2017-07-26 08:07  Beserious  阅读(181)  评论(0编辑  收藏  举报