-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateParenthesis.java
More file actions
executable file
·37 lines (33 loc) · 1.08 KB
/
generateParenthesis.java
File metadata and controls
executable file
·37 lines (33 loc) · 1.08 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
36
37
package array;
import java.util.ArrayList;
import java.util.List;
/**
* 给 n 对括号,写一个函数生成所有合适的括号组合。
* Created by Administrator on 2018/3/28 0028.
*/
public class generateParenthesis {
public static List<String> generateParenthesis(int n) {
// Write your code here
List<String> res = new ArrayList<String>();
String item = new String();
getgenerateParentList(res, item, n, 0, 0);
return res;
}
public static void getgenerateParentList(List<String> res, String item,
int n, int lge ,int rge) {
if(lge==n){
for(int i=0;i<n-rge;i++){
item += ")";
}
res.add(new String(item));
return;
}
getgenerateParentList(res, item+"(", n, lge+1, rge);
if(lge>rge){
getgenerateParentList(res, item+")", n, lge, rge+1);
}
}
public static void main(String[] args) {
System.out.println(generateParenthesis(5));
}
}