forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-parentheses.cpp
65 lines (62 loc) · 1.94 KB
/
generate-parentheses.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
// Time: O(4^n / n^(3/2)) ~= Catalan numbers
// Space: O(n)
// iterative solution
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string curr;
vector<tuple<int, int, int, char>> stk = {{1, n, n, 0}};
while (!stk.empty()) {
const auto [step, left, right, c] = move(stk.back()); stk.pop_back();
if (step == 1) {
if (left == 0 && right == 0) {
result.emplace_back(curr);
}
if (left < right) {
stk.emplace_back(3, 0, 0, 0);
stk.emplace_back(1, left, right - 1, 0);
stk.emplace_back(2, 0, 0, ')');
}
if (left > 0) {
stk.emplace_back(3, 0, 0, 0);
stk.emplace_back(1, left - 1, right, 0);
stk.emplace_back(2, 0, 0, '(');
}
} else if (step == 2) {
curr.push_back(c);
} else if (step == 3) {
curr.pop_back();
}
}
return result;
}
};
// Time: O(4^n / n^(3/2)) ~= Catalan numbers
// Space: O(n)
// recursive solution
class Solution2 {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string curr;
generateParenthesisRecu(n, n, &curr, &result);
return result;
}
private:
void generateParenthesisRecu(int left, int right, string *curr, vector<string> *result) {
if (left == 0 && right == 0) {
result->emplace_back(*curr);
}
if (left > 0) {
curr->push_back('(');
generateParenthesisRecu(left - 1, right, curr, result);
curr->pop_back();
}
if (left < right) {
curr->push_back(')');
generateParenthesisRecu(left, right - 1, curr, result);
curr->pop_back();
}
}
};