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 ...
随机推荐
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题
Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...
- 本地hosts文件IP地址解析
localhost是一个域名,127.0.0.1为IP地址.Windows系统中,约定127.0.0.1为本地IP地址.localhost是其对应的域名.配置是在hosts文件中设置的,Windows ...
- inline关键字的作用
一.在C&C++中,inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 如下面一宏定义表达式: #define express(v1,v2) (v1 ...
- webdings 和 wingdings 字体
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- The STM32 SPI and FPGA communication
The STM32 SPI and FPGA communication STM32 spi bus communication SPI bus in the study, the protocol ...
- STM32输入捕获模式设置并用DMA接收数据
参考: STM32的PWM输入模式设置并用DMA接收数据 Input capture mode The input stage samples the corresponding TIx input ...
- ElasticSearch入门 :Windows下安装ElasticSearch
这是ElasticSearch 2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- [译] Go 并发编程基础
原文:Fundamentals of concurrent programming 译者:youngsterxyf 本文是一篇并发编程方面的入门文章,以Go语言编写示例代码,内容涵盖: 运行期并发线程 ...
- wifidog交叉编译
本文主要记录在linux平台下.交叉编译wifidog并在openwrt平台上执行的过程.主要是针对wifidog源代码被改动后. 不得不亲自进行交叉编译移植的时候,所碰到的一些问题. (1)下载源代 ...
- delphi TOnFormVisibleChangeEvent 事件应用
TGQIFileMgrForm = class(TForm) 定义 property OnVisibleChange: TOnFormVisibleChangeEvent read FOnVisibl ...