-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate Parenthses
More file actions
43 lines (24 loc) · 790 Bytes
/
Generate Parenthses
File metadata and controls
43 lines (24 loc) · 790 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
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
Example 2:
Input: n = 1
Output: ["()"]
class Solution {
public:
void generate(string s,vector<string> &res,int open,int close,int max){
if(s.length()>=max*2){
res.push_back(s);
return;
}
if(open <max) generate(s+"(",res,open+1,close,max);
if(close<open) generate(s+")",res,open,close+1,max);
}
vector<string> generateParenthesis(int n) {
vector<string> res;
string s="";
generate(s,res,0,0,n);
return res;
}
};