-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.java
More file actions
36 lines (32 loc) · 899 Bytes
/
GenerateParentheses.java
File metadata and controls
36 lines (32 loc) · 899 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
import java.util.ArrayList;
import java.util.List;
public class GenerateParentheses {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
boolean[] position = new boolean[2 * n];
recursive(0, 0, n, position, result);
return result;
}
private static void recursive(int index, int point, int max, boolean[] position, List<String> result){
boolean isOver = false;
if (index == max - 1)
isOver = true;
for (int i = point; i < 2 * index + 1; i++) {
position[i] = true;
if (isOver) {
StringBuilder str = new StringBuilder();
for (int l = 0; l < position.length; l++) {
if (position[l]) {
str.append("(");
}else {
str.append(")");
}
}
result.add(str.toString());
}else {
recursive(index + 1, i + 1, max, position, result);
}
position[i] = false;
}
}
}