1+ 递归 。
2+ 看thought .取或者不取 (, )
3+ ```
4+ /*
5+ Generate Parentheses
6+
7+ Given n pairs of parentheses,
8+ write a function to generate all combinations of well-formed parentheses.
9+
10+ Example
11+ Given n = 3, a solution set is:
12+
13+ "((()))", "(()())", "(())()", "()(())", "()()()"
14+
15+ Tags Expand
16+ String Backtracking Recursion Zenefits Google
17+
18+ */
19+
20+ /*
21+ Thoughts:
22+ //http://fisherlei.blogspot.com/2012/12/leetcode-generate-parentheses.html
23+ Either put ( or )
24+ can only put ( when # of ( < n
25+ can only put ) when # of ) < # of (
26+ If # of single '(' > 0, then we can put ')'
27+ If n > 0, we can split: 1. close it with ')'; or 2. add '('
28+ when n-- becomese = 0 and #p = 0, rst.add
29+
30+ */
31+ public class Solution {
32+ /**
33+ * @param n n pairs
34+ * @return All combinations of well-formed parentheses
35+ */
36+ ArrayList <String > rst = new ArrayList <String >();
37+ public ArrayList <String > generateParenthesis (int n ) {
38+ if (n <= 0 ) {
39+ return rst ;
40+ }
41+ ArrayList <String > list = new ArrayList <String >();
42+ helper (list , 0 , 0 , n );
43+ return rst ;
44+ }
45+
46+ public void helper (ArrayList <String > list , int left , int right , int n ) {
47+ if (left == n && right == n ) {
48+ StringBuffer sb = new StringBuffer ();
49+ for (String s : list ) {
50+ sb .append (s );
51+ }
52+ rst .add (sb .toString ());
53+ return ;
54+ }
55+ if (left < n ) {
56+ list .add ("(" );
57+ helper (list , left + 1 , right , n );
58+ list .remove (list .size () - 1 );
59+ }
60+ if (right < left ) {
61+ list .add (")" );
62+ helper (list , left , right + 1 , n );
63+ list .remove (list .size () - 1 );
64+ }
65+ }
66+ }
67+
68+
69+ /*
70+ //
71+ 1st attempt, timeout.
72+ Thoughts:
73+ n = 0, null
74+ n = 1, trivial: ()
75+ Do i-- from n. For each i >= 2
76+ it can choose: close a paren
77+ open another paren
78+ front = (
79+ end = )
80+ helper(front, end, int n)
81+ if (n == 1) {
82+ front + "()" + end
83+ rst.add // check duplicate
84+ }
85+
86+ */
87+ public class Solution {
88+ /**
89+ * @param n n pairs
90+ * @return All combinations of well-formed parentheses
91+ */
92+ public ArrayList <String > rst = new ArrayList <String >();
93+ public ArrayList <String > generateParenthesis (int n ) {
94+ if (n <= 0 ) {
95+ return rst ;
96+ } else if (n == 1 ){
97+ rst .add ("()" );
98+ return rst ;
99+ }
100+ helper ("" , "" , n );
101+ Collections .sort (rst );
102+ return rst ;
103+ }
104+ //3
105+ public void helper (String front , String end , int n ) {
106+ if (n == 1 ) {
107+ String rt = front + "()" + end ;
108+ if (!rst .contains (rt )){
109+ rst .add (rt );
110+ }
111+ return ;
112+ }
113+ n --;
114+
115+ helper (front + "(" , ")" + end , n );
116+ helper (front + "()" , end , n );
117+ helper (front , "()" + end , n );
118+ helper (front + end + "(" , ")" , n );
119+ helper (front + end , "()" , n );
120+ helper (front + end + "()" , "" , n );
121+ helper ("(" , ")" + front + end , n );
122+ helper ("()" , front + end , n );
123+ helper ("" ,"()" + front + end , n );
124+ helper ("(" ,front +end +")" ,n );
125+ helper ("(" + front +end ,")" ,n );
126+ helper ("(" + front , end + ")" ,n );
127+ helper ("(" +front +end +")" , "" , n );
128+ helper ("" , "(" +front +end +")" , n );
129+
130+ }
131+
132+ }
133+
134+ ```
0 commit comments