c++ solution


  • 1
    B
    class Solution {
    public:
        vector<string> result;
        
        void dfs(string cur, int l, int r){
            if(!l && !r) {
                result.push_back(cur);
                return;
            }
            else{
                if(l>0) dfs(cur+"(",l-1,r);
                if(r>0 && l<r) dfs(cur+")",l,r-1);
            }
            return;
        }
    
        vector<string> generateParenthesis(int n) {
            dfs("",n,n);
            return result;
        }
    };
    

Log in to reply
 

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