forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParenthesis.java
More file actions
39 lines (30 loc) · 796 Bytes
/
GenerateParenthesis.java
File metadata and controls
39 lines (30 loc) · 796 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
import java.util.ArrayList;
import java.util.List;
/**
* Description:
* User: liqing@pluosi
* Date: 2020-11-16
* Time: 11:02 PM
*/
public class GenerateParenthesis {
private List<String> result;
public List<String> generateParenthesis(int n) {
result = new ArrayList<String>();
_generate(0,0,n,"");
return result;
}
private void _generate(int left, int right,int n, String s){
if(left==n && right == n){
result.add(s);
}
if(left<n){
_generate(left+1,right,n,s+"(");
}
if(left > right){
_generate(left,right+1,n,s+")");
}
}
public static void main(String[] args) {
System.out.println(new GenerateParenthesis().generateParenthesis(3));
}
}