美文网首页
171. Excel Sheet Column Number

171. Excel Sheet Column Number

作者: SilentDawn | 来源:发表于2018-07-07 16:53 被阅读0次

    Problem

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

    Example

    For example:

        A -> 1
        B -> 2
        C -> 3
        ...
        Z -> 26
        AA -> 27
        AB -> 28 
        ...
    
    Input: "A"
    Output: 1
    
    Input: "AB"
    Output: 28
    
    Input: "ZY"
    Output: 701
    

    Code

    static int var = [](){
        std::ios::sync_with_stdio(false);
        cin.tie(NULL);
        return 0;
    }();
    class Solution {
    public:
        int titleToNumber(string s) {
            int res = 0;
            for(int i=0;i<s.size();i++){
                res = res*26 + (s[i]-64);
            }
            return res;
        }
    };
    

    Result

    171. Excel Sheet Column Number.png

    相关文章

      网友评论

          本文标题:171. Excel Sheet Column Number

          本文链接:https://www.haomeiwen.com/subject/ovqguftx.html