-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-22-Generate-Parentheses.java
65 lines (53 loc) · 1.86 KB
/
LeetCode-22-Generate-Parentheses.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
65
/*
1.Recursive (DFS)
2.Iterative (BFS)
3.DP
*/
public class Solution {
// 1. DFS
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
if(n <= 0) return result;
DFS(result, "", 0, 0, n);
return result;
}
private void DFS(List<String> result, String str, int open, int close, int max){
//end condition
if(str.length() == max * 2){
result.add(str);
return;
}
if(open < max) DFS(result, str + "(", open + 1, close, max);
if(close < open) DFS(result, str + ")", open, close + 1, max); //Notice: close < open, to avoid ")))((("
}
// 2.BFS
/*
https://leetcode.com/problems/generate-parentheses/discuss/10337/My-accepted-JAVA-solution
Runtime: 2 ms, faster than 21.72% of Java online submissions for Generate Parentheses.
slow
*/
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
if (n == 0) {
result.add("");
} else {
for (int i = n - 1; i >= 0; i--) {
List<String> insertSub = generateParenthesis(i);
List<String> tailSub = generateParenthesis(n - 1 - i);
// System.out.println(insertSub);
// System.out.println(tailSub);
// System.out.println(result);
for (String insert : insertSub) {
for (String tail : tailSub) {
result.add("(" + insert + ")" + tail);
}
}
}
}
return result;
}
// 3.DP
/*
https://leetcode.com/problems/generate-parentheses/discuss/10162/*Java*-Easy-to-understand-recursive-DP-method-with-explanations
*/
}