-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateParentheses.java
More file actions
37 lines (32 loc) · 957 Bytes
/
GenerateParentheses.java
File metadata and controls
37 lines (32 loc) · 957 Bytes
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
package com.leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jamylu on 2018/1/7.
* leetcode022.
* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
*/
public class GenerateParentheses {
public static void main(String[] args) {
int n = 3;
System.out.println(generate(n));
}
//回溯法
public static List<String> generate(int n) {
List<String> ans = new ArrayList<>();
backtrack(ans, "", 0, 0, n);
return ans;
}
public static void backtrack(List<String> ans, String cur, int open, int close, int max) {
if (cur.length() == max * 2) {
ans.add(cur);
return;
}
if (open < max) {
backtrack(ans, cur + "(", open + 1, close, max);
}
if (close < open) {
backtrack(ans, cur + ")", open, close + 1, max);
}
}
}