LeetCode 22 Generate Parentheses
2015-06-13 00:31
465 查看
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:
DFS 参考:http://blog.csdn.net/yangliuy/article/details/41170599
View Code
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
DFS 参考:http://blog.csdn.net/yangliuy/article/details/41170599
public class Solution { public static List<String> ans; public List<String> generateParenthesis(int n) { ans=new ArrayList<String>(); if(n<=0) return ans; dfs("",n,n); return ans; } public void dfs(String str,int left,int right){ if(left>right){ return ; } if(left==0&&right==0){ ans.add(str); } if(left>0){ dfs(str+"(",left-1,right); } if(right>0){ dfs(str+")",left,right-1); } } }
View Code
相关文章推荐
- 使用kickstart服务全自动安装RHEL7.0系统
- 详解如何根据网站PV选择主机带宽
- 字符串截取,一个中文算两个字符
- POJ 1065 解题报告
- mac平台下面ruby环境搭建
- Codeforces Round #307 (Div. 2)
- linux下安装jdk1.7
- JNI学习笔记
- 运算符
- Android事件传递机制
- 关于innodb purge thread和master thread
- ConfuserEx
- [LeetCode][JavaScript]Implement Stack using Queues
- linux安装jdk
- ubuntu10 系统root用户无法正常使用tab键补全命令
- tomcat进行redeploy时出错:could not be completely deleted
- 毕业论文从0到完成获得东西
- Uva - 10763 - Foreign Exchange
- Uva - 10763 - Foreign Exchange
- Android 真机连接本地PC服务器