-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.cpp
57 lines (53 loc) · 1.41 KB
/
22.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
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
findParenthesis(result, "",n,n);
return result;
}
private:
void findParenthesis(vector<string> &result, string already, int leftNum, int rightNum){
if(leftNum==0 && rightNum==0){
result.push_back(already);
return;
}
int sum = leftNum + rightNum;
for(int i=0;i<sum;i++){
if(leftNum>0){
string update = already+'(';
findParenthesis(result, update, leftNum-1, rightNum);
}
if(rightNum>0){
string update = already+')';
if(!violate(update)){
findParenthesis(result, update, leftNum, rightNum-1);
}
}
}
}
bool violate(string str){
int leftNum=0, rightNum=0;
for(int i=0;i<str.length();i++){
if(str[i]=='('){
leftNum++;
}else{
rightNum++;
}
if(rightNum > leftNum) return true;
}
return false;
}
};
int main(){
Solution s;
std::vector<string> v = s.generateParenthesis(3);
for(int i=0;i<v.size();i++){
cout << v[i] << endl;
}
cout << v.size()<<endl;
return 0;
}