-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.括号生成.cpp
48 lines (47 loc) · 957 Bytes
/
22.括号生成.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* @lc app=leetcode.cn id=22 lang=cpp
*
* [22] 括号生成
*
* https://leetcode-cn.com/problems/generate-parentheses/description/
*
* algorithms
* Medium (69.01%)
* Total Accepted: 16.6K
* Total Submissions: 24K
* Testcase Example: '3'
*
* 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
*
* 例如,给出 n = 3,生成结果为:
*
* [
* "((()))",
* "(()())",
* "(())()",
* "()(())",
* "()()()"
* ]
*
*
*/
class Solution
{
public:
vector<string> out;
vector<string> generateParenthesis(int n)
{
string s="";
gen(s, 0, 0, n);
return out;
}
void gen(string s, int l, int r, int n)
{
if(l == n && r == n) out.push_back(s);
else if(l>=r && l<=n)
{
gen(s+"(", l+1, r, n);
gen(s+")", l, r+1, n);
}
}
};