forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
46 lines (33 loc) · 881 Bytes
/
main2.cpp
File metadata and controls
46 lines (33 loc) · 881 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
37
38
39
40
41
42
43
44
45
46
/// Source : https://leetcode.com/problems/generate-parentheses/description/
/// Author : liuyubobobo
/// Time : 2018-09-23
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
/// Generate all valid permutation directly
/// Time Complexity: O(2^n)
/// Space Complexity: O(n)
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
generate(n, n, "", res);
return res;
}
private:
void generate(int left, int right, const string& cur, vector<string>& res){
if(left == 0 && right == 0){
res.push_back(cur);
return;
}
if(left)
generate(left - 1, right, cur + '(', res);
if(right && left < right)
generate(left, right - 1, cur + ')', res);
}
};
int main() {
return 0;
}