Home LeetCode - 171. Excel Sheet Column Number
Post
Cancel

LeetCode - 171. Excel Sheet Column Number

171. Excel Sheet Column Number - easy

문제

Given a column title as appear in an Excel sheet, return its corresponding column number.

1
2
3
4
5
6
7
8
9
10
For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

제한사항

  • 1 <= s.length <= 7
  • s consists only of uppercase English letters.
  • s is between “A” and “FXSHRXW”.

입출력 예

1
2
3
4
Example 1:

Input: "A"
Output: 1
1
2
3
4
Example 2:

Input: "AB"
Output: 28
1
2
3
4
Example 3:

Input: "ZY"
Output: 701

풀이

  • Math
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
    int titleToNumber(string s) {
        int result = 0;
        
        for(auto i = 0 ; i < s.size() ; ++i)
            result += pow(26, (s.size() - i) - 1) * ((int)(s[i] - 'A') + 1);

        return result;
    }
};
This post is licensed under CC BY 4.0 by the author.