Leetcode 22. 括号生成

题目描述:数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。

递归生成括号即可。

class Solution {
public:
    string s;
    vector<string>res;
    int count1=0;
    int count2=0;
    char c[2] = {
        '(',
        ')'
    };
    void addPat(int n,int depth)
    {
        if(depth == n*2&&count1==n)
        {
            res.push_back(s);
            return;
        }
        if(count2==n||count1 == n+1||count1<count2)
        {
            return;
        }  
        s.push_back('(');
        count1++;
        addPat(n,depth+1);
        count1--;
        s.pop_back();
        s.push_back(')');
        count2++;
        addPat(n,depth+1);
        count2--;
        s.pop_back();
    }
    vector<string> generateParenthesis(int n) {
        addPat(n,0);
        return res;
    }
};