Simple C++ sol backtracking


  • 5
    M

    class Solution {

    private:

    vector<string> res;

    public:

    void genP(int lc, int rc, string s){
        if(lc == 0 && rc ==0){
            res.push_back(s);
            return;
        }
        if(lc>0)
            genP(lc-1,rc,s+'(');
        if(rc>lc)
            genP(lc,rc-1,s+')');
    }
    vector<string> generateParenthesis(int n) {
        genP(n,n,"");
        return res;
    }
    

    };


Log in to reply
 

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