0ms neat c++ recursive solution, easy to understand


  • 4
    I
    void help(vector<string>& parentheses, string cur, int leftCount, int n) {
        if (leftCount > cur.size() - leftCount) {
    	    help(parentheses, cur + ")", leftCount, n);
        }
        if (leftCount < n) {
    	    help(parentheses, cur + "(", leftCount + 1, n);
        }
        if (cur.size() == n * 2) {
    	    parentheses.push_back(cur);
        }
    }
    
    vector<string> generateParenthesis(int n) {
        vector<string> parentheses;
        help(parentheses, "", 0, n);
        return parentheses;
    }

Log in to reply
 

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