-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.java
More file actions
executable file
·38 lines (33 loc) · 1.18 KB
/
Copy pathGenerateParentheses.java
File metadata and controls
executable file
·38 lines (33 loc) · 1.18 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
package recursion;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Mr.Z
* @DateTime: 2021/01/25 20:52
* @Description: 22. 括号生成 https://leetcode-cn.com/problems/generate-parentheses/
* <p>
* 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
* 输入:n = 3
* 输出:["((()))","(()())","(())()","()(())","()()()"]
**/
public class GenerateParentheses {
private List<String> result;
public List<String> generateParenthesis(int n) {
result = new ArrayList<String>();
generate(0, 0, n, "");
return result;
}
private void generate(int left, int right, int n, String s) {
if (left == n && right == n) {
result.add(s);
return;
}
// left<n 左括号随意加,right<left 右括号随意加
if (left < n) generate(left + 1, right, n, s + "(");
if (right < left) generate(left, right + 1, n, s + ")");
}
public static void main(String[] args) {
GenerateParentheses g = new GenerateParentheses();
g.generateParenthesis(3);
}
}