-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_22.java
More file actions
52 lines (46 loc) · 1.44 KB
/
Copy pathP_22.java
File metadata and controls
52 lines (46 loc) · 1.44 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package leetcode.medium;
import java.util.ArrayList;
import java.util.List;
public final class P_22 {
public List<String> generateParenthesis(int n) {
final List<String> res = new ArrayList<>();
dfs(res, n, n, new StringBuilder());
return res;
}
private static void dfs(List<String> res, int open, int close, StringBuilder curr) {
if (open == 0 && close == 0) {
res.add(curr.toString());
return;
}
if (close > open) {
curr.append(')');
dfs(res, open, close - 1, curr);
curr.deleteCharAt(curr.length() - 1);
}
if (open > 0) {
curr.append('(');
dfs(res, open - 1, close, curr);
curr.deleteCharAt(curr.length() - 1);
}
}
public List<String> generateParenthesisCatalan(int n) {
return dfs(1, n);
}
public List<String> dfs(int start, int end) {
final List<String> res = new ArrayList<>();
if (start > end) {
res.add("");
return res;
}
for (int i = start; i <= end; i++) {
final List<String> leftNodes = dfs(start, i - 1);
final List<String> rightNodes = dfs(i + 1, end);
for (String left : leftNodes) {
for (String right : rightNodes) {
res.add('(' + left + ')' + right);
}
}
}
return res;
}
}