Leetcode [22] (Java): Generate parentheses

Source: Internet
Author: User

title : Manufacturing parentheses sequence

Difficulty : Medium

topic content :

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

translation :

Given n pairs of parentheses, write a function to generate all the well-formed combinations of parentheses.

For example, given n = 3, a solution set is:

[  "((()))",  "(()())",  "(())()",  "()(())",  "()()()"]

My idea : This should be a typical application of the Cattleya number of the bar, for this output all the possible combination of * *, in the case of a single eye should be considered Cattleya number, is f (n) = f (0) F (n-1) + f (1) f (n-2) + ... + f (n-1) f (0), on this topic should mean, first there are n brackets, a total of 2n symbols, the leftmost symbol can only be "(" and the right parenthesis with its collocation can only appear in the position of 2i, so at this time the first parenthesis divides the whole sequence into two parts (2~2i-1) and all Behind, These two parts can continue to be divided, so there is this formula: F (n) =∑f (i) f (n-1-i)

After determining the number of Cattleya, the recursive method is generally considered.

1      PublicList<string> Generateparenthesis (intN) {2list<string> ans =NewArrayList ();3         if(n = = 0) {4Ans.add ("");5}Else {6              for(inti = 0; I < n; I++)7                  for(String in:generateparenthesis (i))8                      for(String out:generateparenthesis (n-1-i))9Ans.add ("(" + In + ")" +Out );Ten         } One         returnans; A}

my complexity : Time: O (2n!) /(n! (n+1)) ) Space: O (2n!) /(n! (n+1)) The number of recursion is the number of calculations, that is, the formula of Cattleya number.

Problems encountered during coding:

1, in the write traversal in, and out, the beginning is also written a list<string> to connect the function results, and then refer to the next answer above, just think for the back of the list can be used directly using the foreach traversal

Reference Answer code :

1      PublicList<string> Generateparenthesis (intN) {2list<string> ans =NewArrayList ();3Backtrack (ans, "", 0, 0, n);4         returnans;5     }6 7      Public voidBacktrack (list<string> ans, String cur,intOpenintCloseintmax) {8         if(cur.length () = = Max * 2) {9 ans.add (cur);Ten             return; One         } A  -         if(Open <max) -Backtrack (ans, cur+ "(", open+1, Close, max); the         if(Close <Open) -Backtrack (ans, cur+ ")", open, close+1, max); -}

Reference Answer complexity : Time: O (2n!) /(n! (n+1)) ) Space: O (2n!) /(n! (n+1)) )

Refer to the answer: using another method, not with the idea of Cattleya number, but using the classic recursive thought "take a step to see", so that people who do not know the Cattleya number easier to read:

Each time a symbol is added, it is divided into two different cases.

A, if the number of parentheses opened "(" less than the number of parentheses to open, and then open a parenthesis "+ (", will have opened the parentheses number + +, and call the next layer;

B, closing parentheses ")" is less than the number of parentheses that have been opened, then a parenthesis "+" will be closed, and a number of + + is closed, and the next layer is called.

Finally, when STR is twice times the length of N, it is added to the result set and returned.

In this way, every situation can be taken into account.

Leetcode [22] (Java): Generate parentheses

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.