22. Generate Parentheses
2016-01-06 22:02
218 查看
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:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
深度优选搜索。
For example, given n = 3, a solution set is:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
深度优选搜索。
public class Solution { public List<String> generateParenthesis(int n) { if(n <= 0) return null; ArrayList<String> list = new ArrayList<String>(); String str = new String(); dfs(list,str,n,n); return list; } private void dfs(ArrayList<String> list, String str, int left, int right){ if(left > right) return;//problem with ")(" if(left == 0 && right == 0){ list.add(str); } if(left > 0) dfs(list,str+"(",left-1,right); if(right > 0) dfs(list, str+")",left,right-1); } }
相关文章推荐
- 4. 【创建和销毁对象】通过私有化构造方法强化不可实例化的能力
- 自定义NSOperation进行多线程之间的通信(通知、代理、block)
- 个人学习c++的真实经验
- 并行和并发
- FOUNDATION的基本介绍
- 周易六十四卦——需卦
- js中对字符串的处理
- mysql从零开始(二)对数据表的操作
- 浏览器引擎(内核)介绍
- asp.net身份验证方式
- lintcode :maximum subarray difference 最大子数组差
- linux命令之find (1)
- [转]linux中添加ftp用户,并设置相应的权限
- ECMAscript Function 类型
- StringBuffer类方法的运用
- 在树莓派B+上编译安装opencv 3.1.0 for both python
- Windows下ZooKeeper安装和测试
- 1、IO输入&输出流 简介
- 欢迎使用CSDN-markdown编辑器
- 23. Merge k Sorted Lists