c++ Dp method


  • 0
    G

    class Solution {
    public:
    vector<string> generateParenthesis(int n) {
    vector<set<string>> dp(n+1);
    vector<string> res;
    dp[1].insert("()");
    for(int i = 2; i <= n; ++i){
    for(auto &str : dp[i-1]){
    dp[i].insert("(" + str + ")");
    }
    for(int j = 1; j <= i-1; ++j){
    for(auto &s1 : dp[j]){
    for(auto &s2 : dp[i-j]){
    dp[i].insert(s1 + s2);
    }
    }
    }
    }
    for(auto &str : dp[n]){
    res.push_back(str);
    }
    return res;
    }
    };


Log in to reply
 

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