-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path22. Generate Parentheses.cpp
More file actions
35 lines (33 loc) · 1.22 KB
/
Copy path22. Generate Parentheses.cpp
File metadata and controls
35 lines (33 loc) · 1.22 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
class Solution {
public:
//https://leetcode.com/problems/generate-parentheses/
void parensHelper(int left,int right,int n,vector<string>&result,string temp){
if(right==n){
//右边等于n说明满了,插入temp,返回
result.push_back(temp);
return;
}
/*
if(left>=n)
return;*/ //这里不能加上这一句话,一旦加上,假使left>n,return之后就不会在之后添加)了
if(left<n){//这里不能给写成left<right,因为一开始right=0
//深度优先
temp+='(';
parensHelper(left+1,right,n,result,temp);
temp.pop_back();
}
//到现在,尽管dfs题目做的很少,但是发现每每深度优先搜索之后,一定有“出栈”,方便下一个入口函数
if(left>right){
temp+=')';
parensHelper(left,right+1,n,result,temp);
temp.pop_back();
}
} // ( ( ( ) ) ) -- ( ( ( ) ) ) --
vector<string> generateParenthesis(int n){
string temp;
vector<string> result;
parensHelper(0,0,n,result,temp);
return result;
}
};
// if(left>right) 在这边出错啊