-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.cpp
More file actions
46 lines (44 loc) · 1000 Bytes
/
Copy pathGenerateParentheses.cpp
File metadata and controls
46 lines (44 loc) · 1000 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
41
42
43
44
45
46
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n==0)
return vector<string>();
vector<string> ret;
string str;
generate(n,n,ret,str);
return ret;
}
void generate(int left,int right,vector<string> &ret,string &str)
{
if(left>right)
return;
if(left==0&&right==0)
{
ret.push_back(str);
return;
}
if(left>0)
{
str.push_back('(');
generate(left-1,right,ret,str);
str.pop_back();
}
if(right>0)
{
str.push_back(')');
generate(left,right-1,ret,str);
str.pop_back();
}
}
};
int main()
{
Solution s;
vector<string> result=s.generateParenthesis(3);
for(auto a:result)
cout<<a<<endl;
}