Topic:
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
Translation:
Give you an n, generate n set of parentheses, rules that conform to parentheses
Ideas:
This problem before the data structure has been done, and the number of the stack is related.
The name is called Cattleya number.
Baidu Encyclopedia Viki
Attach some Daniel's explanations about the number of Cattleya.
Http://www.cnblogs.com/wuyuegb2312/p/3016878.html
Next, say what I think. This problem can actually be done by hand. Each time the string is pressed into the list, the end condition is that both the left and right brackets have the remaining 0. And in the recursive process, the number of left parenthesis is greater than or equal to the right parenthesis.
Code:
public static list<string> generateparenthesis (int n) { list<string> ls = new arraylist<string> () ; if (n <= 0) return ls; Gen (N,n, "", LS); return ls; } public static void Gen (int l,int r,string s,list<string>ls) {if (R < L) return; if (L==0&&r = = 0) Ls.add (s); if (L > 0) gen (l-1,r,s+ ' (', LS); if (R > 0) gen (l,r-1,s+ ') ', LS);}
LEETCODE22 Generate Parentheses Bracket generation