LeetCode22 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 ,生成N组括号,符合括号的规则

思路:

这道题以前学数据结构的时候曾经做过,和出栈的个数有关系。

名字叫做卡特兰数。

百度百科    viki

附上一些大牛的关于卡特兰数的讲解。

http://www.cnblogs.com/wuyuegb2312/p/3016878.html

接下来说我的想法。这道题其实可以按照递归去做。每次把字符串压入到List的结束条件是左括号和右括号剩余都为0。且在递归过程中,保持左括号的个数大于等于右括号即可。

代码:

    	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);
	}

时间: 2024-07-31 15:23:23

LeetCode22 Generate Parentheses 括号生成的相关文章

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

leetcode22. 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

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

【LeetCode-面试算法经典-Java实现】【022-Generate Parentheses(生成括号)】

[022-Generate Parentheses(生成括号)] [LeetCode-面试算法经典-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: "((()))", "(()())&qu

【leetcode-22】括号生成

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()"] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/generate-parentheses 回溯法 回溯算法基本思想:能进则

LeetCode: Generate Parentheses [021]

[题目] 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

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Generate Parentheses Total Accepted: 11470 Total Submissions: 37301 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"