-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathp022.cpp
More file actions
36 lines (34 loc) · 937 Bytes
/
p022.cpp
File metadata and controls
36 lines (34 loc) · 937 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
class Solution {
public:
void f(vector<string> &result, char *c, const int pos, const int len, const int leftbracnum, const int halflen)
{
if (pos == len)
{
if (leftbracnum == 0)
{
c[pos] = '\0';
result.push_back(c);
}
return;
}
else
{
if (leftbracnum < halflen)
{
c[pos] = '(';
f(result, c, pos+1, len, leftbracnum+1, halflen);
}
if (leftbracnum > 0)
{
c[pos] = ')';
f(result, c, pos+1, len, leftbracnum-1, halflen);
}
}
}
vector<string> generateParenthesis(int n) {
char *newstring = new char[n*2+1];
vector<string> result;
f(result, newstring, 0, n*2, 0, n);
return result;
}
};