Neat and concise C++ soln


  • -5
    V

    class Solution {
    public:
    vector<string> ans;
    void gen(int l,int r, string temp){
    if(l==0 && r==0)
    ans.push_back(temp);
    if(l>0)
    gen(l-1,r,temp+'(');
    if(r>l)
    gen(l,r-1,temp+')');
    }
    vector<string> generateParenthesis(int n) {
    gen(n,n,"");
    return ans;
    }
    };


Log in to reply
 

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