-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate Parentheses.cpp
More file actions
40 lines (39 loc) · 930 Bytes
/
Generate Parentheses.cpp
File metadata and controls
40 lines (39 loc) · 930 Bytes
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
#include<iostream>
#include<vector>
using namespace std;
void cur(vector<string>& re,string& cur_re,int leftn,int rightn)
{
if (leftn==0&&rightn==0)
{
re.push_back(cur_re);
return;
}
else
{
if (leftn>rightn)
return;
if (leftn!=0)
{
cur_re.push_back('(');
cur(re,cur_re,leftn-1,rightn);
cur_re.erase(cur_re.end()-1);
}
cur_re.push_back(')');
cur(re,cur_re,leftn,rightn-1);
cur_re.erase(cur_re.end()-1);
}
}
vector<string> generateParenthesis(int n) {
vector<string> re;
string cur_re;
cur(re,cur_re,n,n);
return re;
}
int main()
{
vector<string> re;
re=generateParenthesis(3);
for (int i=0;i<re.size();i++)
cout<<re[i]<<endl;
return 0;
}