-
Notifications
You must be signed in to change notification settings - Fork 118
/
GenerateParentheses.java
64 lines (51 loc) · 1.79 KB
/
GenerateParentheses.java
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
58
59
60
61
62
63
64
package Algorithms;
import java.util.ArrayList;
import java.util.Stack;
public class GenerateParentheses {
public static void main(String[] args) {
GenerateParentheses gp = new GenerateParentheses();
gp.generateParenthesis(2);
}
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> rst = new ArrayList<String>();
ArrayList<Character> path = new ArrayList<Character>();
Stack<Character> s = new Stack<Character>();
genParHelp(n, 0, path, s, rst);
System.out.println(rst.toString());
return rst;
}
public void genParHelp(int n, int index, ArrayList<Character> path, Stack<Character> s, ArrayList<String> rst) {
if (path.size() == 2*n) {
// get a string and add it to the string.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2*n; i ++) {
sb.append(path.get(i));
}
rst.add(sb.toString());
return;
}
char[] input = {'(', ')'};
for (int i = 0; i < 2; i++) {
if (i == 0) {
if (s.size() == (2*n - path.size())) { // full of ((( only can add )
continue;
}
s.push('(');
} else {
if (s.isEmpty()) { // when stack is empty, I can't add a ')';
continue;
}
s.pop();
}
path.add(input[i]);
genParHelp(n, index + 1, path, s, rst);
path.remove(path.size() - 1);
if (i == 0) {
s.pop();
} else {
s.push('(');
}
}
return;
}
}