-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateParen-lc22.cpp
More file actions
42 lines (31 loc) · 874 Bytes
/
generateParen-lc22.cpp
File metadata and controls
42 lines (31 loc) · 874 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
/*
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
*/
class Solution {
public:
void genParenUtil(string strSoFar, int lR, int rR, vector<string> & output){
if (lR == 0 && rR == 0) {
output.push_back(strSoFar);
return;
}
if (lR > 0) {
genParenUtil(strSoFar+ "(", lR-1, rR, output);
}
if (lR < rR) {
genParenUtil(strSoFar+ ")", lR, rR-1, output);
}
}
vector<string> generateParenthesis(int n) {
vector<string> output;
genParenUtil("", n,n, output);
return output;
}
};