首页 > 代码库 > [LeetCode] Excel Sheet Column Number

[LeetCode] 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 

给定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