-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateParentheses.java
78 lines (72 loc) · 2.29 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package algorithm.topics.recursion;
import java.util.ArrayList;
import java.util.List;
/**
* <a href="https://leetcode.cn/problems/generate-parentheses>Generate Parentheses</a>
*
* @author marvin
* @version GenerateParentheses.java, v 0.1 2022/10/04 06:40 Exp $
*/
public class GenerateParentheses {
/**
* 自顶向下回溯
*
* @param n
* @return
*/
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
if (n <= 0) {
return ans;
}
backtrace("", n, n, ans);
return ans;
}
/**
* 自低向上递归
*
* @param n
* @return
*/
public List<String> generateParenthesis2(int n) {
List<String> ans = new ArrayList<>();
generate(ans, "", 0, 0, n);
return ans;
}
private void generate(List<String> ans, String str, int open, int close, int max) {
if (str.length() == max * 2) {
ans.add(str);
return;
}
// 左括号数量不大于n, 可以放一个左括号
if (open < max) {
generate(ans, str + "(", open + 1, close, max);
}
// 右括号数量小于左括号数量,可以放一个右括号数量
if (close < open) {
generate(ans, str + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
GenerateParentheses generateParentheses = new GenerateParentheses();
// List<String> result = generateParentheses.generateParenthesis(3);
List<String> result = generateParentheses.generateParenthesis2(3);
result.forEach(System.out::println);
}
private void backtrace(String str, int open, int close, List<String> ans) {
if (open == 0 && close == 0) {
ans.add(str);
return;
}
if (open == close) {
// 剩余左右括号数相等,继续回溯只能用左括号
backtrace(str + "(", open - 1, close, ans);
} else if (open < close) {
// 剩余左括号数小于右括号,继续回溯可以用左括号 也可以用右括号
if (open > 0) {
backtrace(str + "(", open - 1, close, ans);
}
backtrace(str + ")", open, close - 1, ans);
}
}
}