Excel Sheet Column Number leetcode

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 

思路简单,这里没有使用pow求n次方,而是使用变量t来保存
int titleToNumber(string s) {
    int sum = s[s.length() - 1] - 'A' + 1;
    int t = 26;
    for (int i = s.length() - 2; i >= 0; --i, t *= 26)
        sum += (s[i] - 'A' + 1) * t;
    return sum;
}

 

posted @ 2016-01-18 20:20  sdlwlxf  阅读(148)  评论(0编辑  收藏  举报