-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.cpp
50 lines (43 loc) · 972 Bytes
/
22.cpp
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
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void rec(string curr, int sum, int left, int right, vector<string> &ret)
{
if (left == 0 && right == 0 && sum == 0)
{
ret.push_back(curr);
}
else
{
// add left
if (left > 0)
{
rec(curr + "(", sum + 1, left - 1, right, ret);
}
// add right
if (right > 0 && sum - 1 >= 0)
{
rec(curr + ")", sum - 1, left, right - 1, ret);
}
}
}
vector<string> generateParenthesis(int n)
{
auto ret = vector<string>();
rec("", 0, n, n, ret);
return ret;
}
};
int main(int argc, char const *argv[])
{
auto sol = new Solution();
auto ret = sol->generateParenthesis(atoi(argv[1]));
for (auto s : ret)
{
cout << s << endl;
}
return 0;
}