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 };

 

posted on 2017-09-14 21:14  玲珑子  阅读(157)  评论(0编辑  收藏  举报

导航