# 括号生成

<p>数字 <code>n</code> 代表生成括号的对数，请你设计一个函数，用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>n = 3<strong><br />输出：</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>n = 1<strong><br />输出：</strong>["()"]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= n <= 8</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
class Solution
{
public:
	void gen(string &p, int lc, int rc, vector<string> &r, int n)
	{
		if (lc > n)
			return;
		if (lc == n && rc == n)
		{
			r.push_back(p);
			return;
		}
		p.push_back('(');
		lc++;
		gen(p, lc, rc, r, n);
		p.pop_back();
		lc--;
		if (lc > rc)
		{
			_________________;
		}
	}
	vector<string> generateParenthesis(int n)
	{
		string p;
		int lc = 0, rc = 0;
		vector<string> r;
		gen(p, lc, rc, r, n);
		return r;
	}
};
```

## template

```cpp
class Solution
{
public:
	void gen(string &p, int lc, int rc, vector<string> &r, int n)
	{
		if (lc > n)
			return;
		if (lc == n && rc == n)
		{
			r.push_back(p);
			return;
		}
		p.push_back('(');
		lc++;
		gen(p, lc, rc, r, n);
		p.pop_back();
		lc--;
		if (lc > rc)
		{
			p.push_back(')');
			rc++;
			gen(p, lc, rc, r, n);
			p.pop_back();
			rc--;
		}
	}
	vector<string> generateParenthesis(int n)
	{
		string p;
		int lc = 0, rc = 0;
		vector<string> r;
		gen(p, lc, rc, r, n);
		return r;
	}
};
```

## 答案

```cpp
p.push_back(')');
rc++;
gen(p, lc, rc, r, n);
p.pop_back();
rc--;
```

## 选项

### A

```cpp
p.push_back(')');
rc--;
gen(p, lc, rc, r, n);
p.pop_back();
rc++;
```

### B

```cpp
p.push_back('(');
rc++;
gen(p, lc, rc, r, n);
p.pop_back();
rc--;
```

### C

```cpp
p.push_back('(');
rc--;
gen(p, lc, rc, r, n);
p.pop_back();
rc++;
```