Easy to understand


  • 0
    Y
    class Solution {
    public:
    vector<string> generateParenthesis(int n) {
        vector<string> strs;
        genParis(strs, "", n, 0);
        return strs;
    }
    
    void genParis(vector<string> &strs, string str, int n, int cnt) {
        if (n == 0 && cnt == 0) {
            strs.push_back(str);
            return;
        }
        if (n > 0) { genParis(strs, str + '(', n - 1, cnt + 1); }
        if (cnt > 0) { genParis(strs, str + ')', n, cnt - 1); }
    }
    };

Log in to reply
 

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