forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain4.cpp
More file actions
67 lines (51 loc) · 1.43 KB
/
main4.cpp
File metadata and controls
67 lines (51 loc) · 1.43 KB
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
/// Source : https://leetcode.com/problems/generate-parentheses/description/
/// Author : liuyubobobo
/// Time : 2018-09-23
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <unordered_set>
using namespace std;
/// Memory Search
/// Time Complexity: O(2^n)
/// Space Complexity: O(2^n)
class Solution {
private:
vector<vector<string>> res;
public:
vector<string> generateParenthesis(int n){
res.clear();
for(int i = 0; i <= n; i ++)
res.push_back(vector<string>());
return generate(n);
}
private:
vector<string> generate(int n) {
if(n == 0)
return {};
if(res[n].size() != 0)
return res[n];
for(int i = 0; i <= n - 1; i ++){
vector<string> left = generate(i);
if(left.size() == 0) left.push_back("");
vector<string> right = generate(n - i - 1);
if(right.size() == 0) right.push_back("");
for(const string& l: left)
for(const string& r: right)
res[n].push_back("(" + l + ")" + r);
}
return res[n];
}
};
void print_vec(const vector<string>& vec){
for(const string& s: vec)
cout << s << " ";
cout << endl;
}
int main() {
print_vec(Solution().generateParenthesis(1));
print_vec(Solution().generateParenthesis(2));
print_vec(Solution().generateParenthesis(3));
return 0;
}