LeetCode: 171 Excel Sheet Column Number(easy)

题目:

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 

代码:

 1 class Solution {
 2 public:
 3     int titleToNumber(string s) {
 4         map<char, int> map;
 5         int num = 1;
 6         int result = 0;
 7         for (char i = 'A'; i<='Z'; i++){
 8             map[i] = num;
 9             num++;
10         }
11         int j = 0;
12         for(auto c = s.crbegin(); c<s.crend(); c++){
13             result = map[*c]*pow(26, j) + result;
14             j++;
15         }
16             
17         return result;
18     }
19 };

猜你喜欢

转载自blog.csdn.net/lxin_liu/article/details/84844169