forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (38 loc) · 962 Bytes
/
main.cpp
File metadata and controls
49 lines (38 loc) · 962 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
47
48
49
/// 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 permutation and check validation
/// Time Complexity: O(n*2^n)
/// Space Complexity: O(n)
class Solution {
public:
vector<string> generateParenthesis(int n) {
string s = string(n, '(') + string(n, ')');
vector<string> res;
do{
if(valid(s))
res.push_back(s);
}while(next_permutation(s.begin(), s.end()));
return res;
}
private:
bool valid(const string& s){
stack<char> stack;
for(char c: s)
if(c == '(')
stack.push(c);
else if(stack.empty())
return false;
else
stack.pop();
return true;
}
};
int main() {
return 0;
}