22. Generate Parentheses生成指定个括号

生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边)

思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号

有点像二叉树的建立过程

/*
    思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号
    判断好两种添加的条件后向后添加就行:
    1.当左边括号不超过括号数n时可以添加左括号
    2.当右括号不超过左括号时可以添加右括号
    用递归依次向下添加就行
    由于这种数据结构比较像二叉树,代码使用二叉树写的,其实完全不需要用二叉树。
     */
    //这里不能写public,要不LeetCode不给通过
    class TreeNode {
        public StringBuilder val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(StringBuilder str) { val = str; }
    }
    List<String> res = new ArrayList<>();
    public List<String> generateParenthesis(int n) {
        if (n < 1)
            return new ArrayList<>();
        StringBuilder s = new StringBuilder("(");
        TreeNode tree = new TreeNode(s);
        helper(tree,n*2);
        return res;
    }
    public TreeNode helper(TreeNode tree,int n)
    {
        //判断是不是添加完了
        StringBuilder temp = tree.val;
        if (temp.length()>=n)
        {
            res.add(new String(temp));
            return null;
        }
        //统计左右括号数
        int l = 0;
        int r = 0;
        for (int i = 0; i < temp.length(); i++) {
            if (temp.charAt(i)==‘(‘)
                l++;
            if (temp.charAt(i)==‘)‘)
                r++;
        }
        //注意这里一定要新建,如果把temp直接赋给left和right的话,他们三个其实是指向同一个堆内存
        StringBuilder left = new StringBuilder(temp);
        StringBuilder right = new StringBuilder(temp);
        left.append(‘(‘);
        right.append(‘)‘);
        //添加条件
        if (r < l)
        {
            tree.right = helper(new TreeNode(right),n);

        }
        if (l < n/2)
        {
            tree.left = helper(new TreeNode(left),n);
        }
        return tree;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8245351.html

时间: 2024-11-05 12:19:08

22. Generate Parentheses生成指定个括号的相关文章

[LeetCode] 22. Generate Parentheses 生成括号

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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses 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: "((()))", "(()())", "(())()", "()(())", "

[CareerCup] 9.6 Generate Parentheses 生成括号

9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.EXAMPLEInput: 3Output: ((())), (()()), (())(), ()(()), ()()() LeetCode上的原题,请参见我之前的博客Generate Parentheses 生成括号. 解法一: class Solution

No.22 Generate Parentheses

No.22 Generate Parentheses 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: "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses(js)

22. Generate Parentheses 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: [ "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses dfs填表

22. Generate Parentheses Medium 2421147FavoriteShare 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: [ "((()))", "(()())", "(())(

[LeetCode] Generate Parentheses 生成括号

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: "((()))", "(()())", "(())()", "()(())", "()()()" 在LeetCo

[Leetcode][Python]22: Generate Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 22: Generate Parentheseshttps://oj.leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For exampl

LeetCode 22. Generate Parentheses (括号生成)

题目标签:Backtracking 建立一个 HashMap 来记录 括号的 数量,利用DFS, 先用 左括号, 在用 右括号, 当 右括号用完的时候 返回.具体看code. Java Solution: Runtime:  1 ms, faster than 85.94 % Memory Usage: 39.5 MB, less than 20.00 % 完成日期:12/12/2019 关键点:HashMap class Solution { HashMap<Character, Intege