My Clean C++ Solution


  • 0
    R
    class Solution {
    public:
        vector<string> generateParenthesis(int n) {
            vector <string> ans;
            
            if (n == 0){
                ans.push_back("");
                return ans;
            }
            
            for (int i=0; i<n; ++i){
                vector<string> s = generateParenthesis(i);
                vector<string> p = generateParenthesis(n-i-1);
                
                for (int j=0; j<s.size(); ++j)
                    for (int k=0; k<p.size(); ++k){
                        ans.push_back('(' + s[j] + ')' + p[k]);
                    }
            }
            
            return ans;
        }
    };
    

Log in to reply
 

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