-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq022_GenerateParentheses.java
More file actions
70 lines (58 loc) · 1.83 KB
/
q022_GenerateParentheses.java
File metadata and controls
70 lines (58 loc) · 1.83 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package leetcode_algorithm;
import java.util.ArrayList;
import java.util.List;
/**
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
*/
public class q022_GenerateParentheses {
public static void main(String[] args) {
System.out.println(generateParenthesis(2));
System.out.println(generateParenthesis(3));
}
/**
* 解法1 (个人解法)
* @param n
* @return
*/
public static List<String> generateParenthesis(int n) {
return appendString("(", 1, n);
}
private static List<String> appendString(String s , int leftCount , int n) {
if (n*2 == s.length() ) {
List<String> list = new ArrayList<>();
list.add(s.toString());
return list;
}
if (leftCount == n) {
return appendString(s+")", leftCount , n);
}else if(leftCount*2 == s.length()){
return appendString(s+"(", ++leftCount, n);
}else {
List<String> s1 = appendString(s+")", leftCount, n);
List<String> s2 = appendString(s+"(", ++leftCount, n);
s1.addAll(s2);
return s1;
}
}
/**
* 解法2 (推荐解法)
* @param n
* @return
*/
public List<String> generateParenthesis1(int n) {
List<String> list = new ArrayList<>();
backtrack(list , "" , 0 , 0 , n);
return list;
}
public void backtrack(List<String> list, String str, int open, int close, int max) {
if (str.length() == max * 2) {
list.add(str);
return;
}
if (open < max)
backtrack(list , str + "(" , open+1 , close , max);
if(close < open)
backtrack(list , str+")" , open , close+1 , max);
}
}