-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.cpp
More file actions
109 lines (87 loc) · 2.08 KB
/
GenerateParentheses.cpp
File metadata and controls
109 lines (87 loc) · 2.08 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
return getParenthesis(n);
}
vector<string> getParenthesis(int n){
if(n==0){
vector<string> par;
par.push_back("");
return par;
}
if(n==1){
vector<string> par;
par.push_back("()");
return par;
}
else{
vector<string> par;
for(int i=0;i<n;i++){
vector<string> first=getParenthesis(i);
vector<string> second=getParenthesis(n-i-1);
for(int f=0;f<first.size();f++){
for(int s=0;s<second.size();s++){
par.push_back("("+first.at(f)+")"+second.at(s));
}
}
}
/*vector<string> par;
for(int i=n-1;i> 0;i--){
string l="",r="";
for(int k=0;k<i;k++){
l.append("(");
r.append(")");
}
for(int x=n-i;x>0;x--){
vector<string> second=getParenthesis(x);
for(int j=0;j<second.size();j++){
par.push_back(l+second.at(j)+r);
}
cout<<"par: "<<par.back()<<endl;
vector<string> temp=getParenthesis(n-x-i);
for(int j=0;j<par.size();j++){
for(int k=0;k<temp.size();k++){
cout<<"------"<<temp.size()<<endl;
par[j].append(temp[k]);
}
}
}}*/
/*vector<string> temp=getParenthesis(n-1);
vector<string> par;
for(int i=0;i<temp.size();i++){
par.push_back("("+temp.at(i)+")");
}
for(int i=n-1;i>=1;i--){
vector<string> first=getParenthesis(i);
vector<string> second=getParenthesis(n-i);
for(int f=0;f<first.size();f++){
for(int s=0;s<second.size();s++){
if(!(f==first.size()-1&&s==second.size()-1)){
par.push_back(first.at(f)+second.at(s));
}
}
}
}
string str="";
for(int i=0;i<n;i++){
str.append("()");
}
par.push_back(str);*/
return par;
}
}
};
int main(){
int n;
cin>>n;
Solution *solution=new Solution();
vector<string> res=solution->generateParenthesis(n);
for(int i=0;i<res.size();i++){
cout<<res.at(i)<<endl;
}
return 0;
}