"Leetcode" generateparentheses

Source: Internet
Author: User

  • idea 1:

    This topic should start with a simple example and then find out the rules.

    1 ()

    2 () (), (())

    3 () () (), (()) (), (() ()), (() (()), ((()), (())

    The rule is this , assuming that we have produced all the combinations of n-1 pairs of parentheses, then on this basis we can produce all the n pairs of parentheses, and we put one ( placed in each n-1 start with the parentheses, and then insert one in each of the parentheses with the right position. This gives us a combination of n -pairs of parentheses.

  • idea 2:

    The idea of recursion is mainly used to ensure that the number of left brackets in a string is greater than or equal to the number of closing parentheses.

    1. If the number of left brackets is not exhausted, then we can continue to place the left parenthesis

    2. If the number of left brackets already placed is greater than the number of closing parentheses already placed, then we can place a closing parenthesis (an illegal combination if the number of closing parentheses placed is greater than the number of left brackets placed)

    So, using DFS in each level of recursion, if the condition is met, put the opening parenthesis, if the condition is satisfied and then put the closing parenthesis

    Package leetcode.doit;

    ??

    Import java.util.ArrayList;

    Import Java.util.Iterator;

    Import java.util.LinkedList;

    Import java.util.List;

    ??

    public class Generateparentheses_my {

    public static void Main (string[] args) {

    ArrayList ret = (ArrayList) generateparenthesis (3);

    ??

    //???????????????? Iterator<string> it = Ret.iterator ();

    //???????????????? while (It.hasnext ()) {

    //???????????????? System.out.println (It.next ());

    //???????????????? }

    ??

    }

    ??

    static list<string> generateparenthesis (int n) {

    list<string> result = new arraylist<string> ();

    if (n <= 0)

    return result;

    string s = new string ();

    DFS (0, 0, N, s, result);

    return result;

    }

    ??

    public static void Dfs (int left, int. right, int n, String s,

    list<string> result) {

    System.out.println (s);

    if (left = = N && left = right) {

    Result.add (s);

    Return

    }

    if (Left < n)

    DFS (left + 1, right, N, S + "(", result);

    if (right < left) {

    DFS (left, right + 1, N, S + ")", result);

    }

    }

    }

    ? ?

"Leetcode" generateparentheses

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.