-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0022_generateParenthesis.cc
59 lines (52 loc) · 1.06 KB
/
Problem_0022_generateParenthesis.cc
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
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
void process(int cur, int rest, int gap, string &path, vector<string> &ans)
{
if (cur == path.length())
{
ans.push_back(path);
return;
}
if (gap > 0)
{
path[cur] = ')';
process(cur + 1, rest, gap - 1, path, ans);
}
if (rest > 0)
{
path[cur] = '(';
process(cur + 1, rest - 1, gap + 1, path, ans);
}
}
vector<string> generateParenthesis(int n)
{
string str(n << 1, ' ');
vector<string> ans;
process(0, n, 0, str, ans);
return ans;
}
};
bool isVectorEuqal(vector<string> a, vector<string> b)
{
return a == b;
}
void testGenerateParenthesis()
{
Solution s;
vector<string> n1 = {"()()()", "()(())", "(())()", "(()())", "((()))"};
vector<string> n2 = {"()"};
EXPECT_TRUE(isVectorEuqal(n1, s.generateParenthesis(3)));
EXPECT_TRUE(isVectorEuqal(n2, s.generateParenthesis(1)));
EXPECT_SUMMARY;
}
int main()
{
testGenerateParenthesis();
return 0;
}