-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0022-generate-parentheses.cpp
More file actions
52 lines (45 loc) · 994 Bytes
/
0022-generate-parentheses.cpp
File metadata and controls
52 lines (45 loc) · 994 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
47
48
49
50
51
52
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class Solution
{
private:
vector<string> ans;
void genStrings(int openCount, int closeCount, string curr, int n)
{
// cout << openCount << " " << closeCount << " " << curr << n << endl;
if (openCount == closeCount && openCount + closeCount == 2*n)
{
ans.push_back(curr);
return;
}
if (openCount < n)
{
genStrings(openCount+1, closeCount, curr + "(", n);
}
if (closeCount < openCount)
{
genStrings(openCount, closeCount + 1, curr + ")", n);
}
}
public:
vector<string> generateParenthesis(int n)
{
genStrings(0, 0, "", n);
return ans;
}
};
void print(vector<string> &arr)
{
for (string s : arr)
{
cout << s << endl;
}
}
int main()
{
Solution s;
vector<string> ans1 = s.generateParenthesis(3);
print(ans1);
}