171. Excel Sheet Column Number

Problem:

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 
    ...

Example 1:

Input: "A"
Output: 1

Example 2:

Input: "AB"
Output: 28

Example 3:

Input: "ZY"
Output: 701

思路

Solution (C++):

int titleToNumber(string s) {
    int sum = 0, n = s.length();
    for (int i = 0; i < n; ++i)
        sum += (int(s[i]) - 64) * pow(26, n-1-i);
    return sum;
}   

性能

Runtime: 4 ms  Memory Usage: 6.1 MB

思路

Solution (C++):


性能

Runtime: ms  Memory Usage: MB

posted @ 2020-03-30 23:10  littledy  阅读(94)  评论(0编辑  收藏  举报