1. 程式人生 > >[LeetCode] Excel Sheet Column Number

[LeetCode] Excel Sheet Column Number

-- += cor span ... num urn respond 累加

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 

給定Excel的表格標題,求相對應的列號。類似於進制轉換,每一位的進制位為26。對給定字符串從右向左進行累加即可。

class Solution {
public:
    int titleToNumber(string s) {
        int sum = 0;
        int t = 0;
        for (int i = s.size() - 1; i >= 0; i--) {
            sum += ((s[i] - A + 1) * pow(26, t++));            
        }
        return sum;
    }
};
// 3 ms

[LeetCode] Excel Sheet Column Number