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:

"((()))", "(()())", "(())()", "()(())", "()()()"

思路:此题也是不算太难的问题,不过刚开始没有思路,慢慢想,最终经过思考,形成递归算法,即从一个括号()开始,到两个括号()()、(()),到三个括号()()(),()(()),(())(),((())),其实是一个把()插入到前面字符串的过程,只不过本题不能重复,所以还需要hashSet去除重复。

具体代码如下:

public class Solution {
    Set<String> set = new HashSet<String>();//用于消除重复值

    public List<String> generateParenthesis(int n) {
        List<String> list = new ArrayList<String>();
        if(n == 0){
            return list;
        }
        list.add("()");//因为n>=1,故list初始化1对
        //循环,每次增加一对(),并返回,直到最后完成
        for(int i = 1; i < n; i++){
            list = addList(list);
        }
        return list;
    }
    //每次增加一对()并返回无重复的新的list
    List<String> addList(List<String> list){
        List<String> al = new ArrayList<String>();
        int size = list.size();//list已有数据个数
        String temp;//暂时字符串
        for(int i = 0; i < size;i++){
            String s = list.get(i);//取出
            temp = "";
            //对取出的字符串循环插入(),不重复即插入list
            for(int j = 0;j < s.length();j++){
                temp = s.substring(0,j) + "()" + s.substring(j);
                if(set.add(temp)){//判断是否已有
                    al.add(temp);
                }
            }
        }
        return al;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-03 03:00:33

leetCode 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,

[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

[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

Java [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: "((()))", "(()())", "(())()", "()(())", "()()()" 解

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

22. Generate Parentheses生成指定个括号

生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像二叉树的建立过程 /* 思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号 判断好两种添加的条件后向后添加就行: 1.当左边括号不超过括号数n时可以添加左括号 2.当右括号不超过左括号时可以添加右括号 用递归依次向下添加就行 由于这种数据结构比较像二叉树,代码使用二叉树写的

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

[LeetCode] 22. Generate Parentheses Java

题目: 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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]