-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.cpp
76 lines (70 loc) · 1.83 KB
/
Solution.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
using namespace std;
class Solution {
public:
// recursive : returning the result back
vector<string> generateParenthesis_recur(int n) {
return this->genp(n, n);
}
vector<string> genp(int l, int r) {
vector<string> res;
if (l == 0 and r == 1) {
res.push_back(")");
return res;
}
if (l > 0) {
for (auto str: genp(l-1, r)) {
str.insert(0,1,'(');
res.push_back(str);
}
}
if (r > 0 and r > l) {
for (auto str: genp(l, r-1)) {
str.insert(0,1,')');
res.push_back(str);
}
}
return res;
}
// backtracing
/* The idea is intuitive.
Use two integers to count the remaining left parenthesis (n)
and the right parenthesis (m) to be added.
At each function call add a left parenthesis if n > 0 and
add a right parenthesis if m>0. Append the result and terminate recursive calls
when both m and n are zero.
*/
vector<string> generateParenthesis(int n) {
vector<string> res;
string s = "";
this->bt(res,n,n,s);
return res;
}
void bt(vector<string> & v, int l, int r, string & str) {
if (l == 0 and r == 0) {
v.push_back(str);
return;
}
if (l > 0) {
str.push_back('(');
this->bt(v, l-1, r, str);
str.pop_back();
}
if (r > l and r > 0) {
str.push_back(')');
this->bt(v, l, r-1, str);
str.pop_back();
}
return;
}
};
int main() {
Solution s;
return 0;
}