C++ 8ms 2 simple solutions


  • 0
    Z
    class Solution {
    public:
    int titleToNumber(string s) {
        return solution1(s, s.length() - 1);    //recursive
        //return solution2(s);    //iterative
    }
    private:
    int solution1(string s, int pos){
        if(pos < 0) return 0;
        return s[pos] - '@' + 26 * solution1(s, pos - 1);
    }
    int solution2(string s){
        int sum = s[0] - '@';
        for(int i = 1; i < s.length(); i++)
            sum = sum * 26 + s[i] - '@';
        return sum;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.