forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
53 lines (41 loc) · 1.23 KB
/
main3.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
/// 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;
/// Recursively call generateParenthesis for smaller n
/// Time Complexity: O(2^n)
/// Space Complexity: O(2^n)
class Solution {
public:
vector<string> generateParenthesis(int n) {
if(n == 0)
return {};
vector<string> res;
for(int i = 0; i <= n - 1; i ++){
vector<string> left = generateParenthesis(i);
if(left.size() == 0) left.push_back("");
vector<string> right = generateParenthesis(n - i - 1);
if(right.size() == 0) right.push_back("");
for(const string& l: left)
for(const string& r: right)
res.push_back("(" + l + ")" + r);
}
return res;
}
};
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;
}