其他分享
首页 > 其他分享> > leetcode 22. Generate Parentheses (backtracking)

leetcode 22. Generate Parentheses (backtracking)

作者:互联网

class Solution {
public:
    vector<string> r;
    vector<string> generateParenthesis(int n) {
        string s = "";
        h(s, n, n);
        return r;
    }
    
    void h(string s, int left, int right) {
        if(left == 0 && right == 0) {
            r.push_back(s);
            return;
        }
        if(left == right) {
            h(s + "(", left - 1, right);
        }
        else if(left < right) {
            if(left > 0) {
                h(s + "(", left - 1, right);
            }
            h(s + ")", left, right - 1);
        }
    }
};

标签:vector,Parentheses,return,string,int,right,leetcode,backtracking,left
来源: https://blog.csdn.net/weixin_55399173/article/details/120870022