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:
"((()))", "(()())", "(())()", "()(())", "()()()"
题解:
这道题跟unique binary tree ii是类似的。如果是只求个数的话是类似unique binary tree,用到了卡特兰数。
这里也是用到了类似的模型。
不过这道题按照DFS那种递归想法解决还是比较容易想到的。
给定的n为括号对,所以就是有n个左括号和n个右括号的组合。
按顺序尝试知道左右括号都尝试完了就可以算作一个解。
注意,左括号的数不能大于右括号,要不然那就意味着先尝试了右括号而没有左括号,类似“)(” 这种解是不合法的。
代码如下:
 1     public ArrayList<String> generateParenthesis(int n) {  
 2         ArrayList<String> res = new ArrayList<String>();
 3         String item = new String();
 4         
 5         if (n<=0)
 6             return res;  
 7             
 8         dfs(res,item,n,n);  
 9         return res;  
     }  
       
     public void dfs(ArrayList<String> res, String item, int left, int right){ 
         if(left > right)//deal wiith ")("
             return;
             
         if (left == 0 && right == 0){  
             res.add(new String(item));  
             return;  
         }
         
         if (left>0) 
             dfs(res,item+'(',left-1,right);  
         if (right>0) 
             dfs(res,item+')',left,right-1);  
     } 
Reference:
http://blog.csdn.net/linhuanmars/article/details/19873463
http://blog.csdn.net/u011095253/article/details/9158429
Generate Parentheses leetcode java的更多相关文章
- Generate Parentheses - LeetCode
		目录 题目链接 注意点 解法 小结 题目链接 Generate Parentheses - LeetCode 注意点 解法 解法一:递归.当left>right的时候返回(为了防止出现 )( ) ... 
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
		回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ... 
- Generate Parentheses——LeetCode
		Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ... 
- Longest Valid Parentheses leetcode java
		题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ... 
- Valid Parentheses leetcode java
		题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ... 
- LeetCode 22. 括号生成(Generate Parentheses)
		22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ... 
- [Leetcode][Python]22: Generate Parentheses
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ... 
- LeetCode: Generate Parentheses 解题报告
		Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ... 
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
		Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ... 
随机推荐
- 3143 二叉树的序遍历codevs
			题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点个数. 接下来n行每行2个整数L和R.第i ... 
- poj 2599 单调栈 ***
			和poj2082差不多,加了一个宽度的条件 #include<cstdio> #include<cmath> #include<algorithm> #includ ... 
- 14、Redis的复制
			写在前面的话:读书破万卷,编码如有神 --------------------------------------------------------------------------------- ... 
- 重读JavaScript高级程序设计
			不断更新中--- 第三章 基本概念 1.变量声明但未初始化值是undefined,而未声明的变量只能执行typeof操作,并且未初始化和未声明用typeof都同样返回undefined 2.Numbe ... 
- PHP之PDO_MYSQL扩展安装步骤(转)
			看到CakePHP文档要求安装pdo_mysql扩展,于是就尝试安装了一下. 这里我的系统是CentOS 6.0.如果你的系统是其他版本的Linux/Unix,可以参考.如果你的系统是Windows的 ... 
- 在.net core 2.0中生成exe文件
			.net core 2.0程序默认生成的是一个dll,需要通过dotnet命令来执行他. dotnet ConsoleApp1.dll 这种方式有点类似于java程序.本身这种方式没有什么问题,但在调 ... 
- High accuracy voltage regulator
			High accuracy voltage regulator Good morning everybody, I want to make a accurate voltage regulator ... 
- nginx源码学习 资料
			首先要做的当然是下载一份nginx源码,可以从nginx官方网站下载一份最新的. 看了nginx源码,发现这是一份完全没有注释,完全没有配置文档的代码. 现在你最希望要的是一份注释版的nginx源码, ... 
- 为什么MacBook装Windows这么火?
			Mac到底要不要装Windows?一直以来这都是个很有争议性的话题.只要你经常浏览国内一些知名Mac论坛,就会发现那里不仅有各种Mac装Windows教学贴.讨论区,而且时不时还会冒出关于“Mac装不 ... 
- lufylegend:加载进度
			实现图片加载进度 LoadingSample Class 用来显示进度条的对象. 引擎中目前提供的进度条类有:LoadingSample1-7 你可以制作自己的进度条,自制进度条类中要包含setPro ... 
