22. Generate Parentheses
2016-03-11 11:12
204 查看
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
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
解题思路:
DFS
class Solution { public: vector<string> generateParenthesis(int n) { vector<string>res; if(n==0)return res; dfs(res,n,0,0,""); return res; } void dfs(vector<string>&res,int n,int l,int r,string s){ if(l==n&&r==n){ res.push_back(s); return ; } if(l==n){ dfs(res,n,l,r+1,s+")"); return ; } if(l==r){ dfs(res,n,l+1,r,s+"("); return ; } dfs(res,n,l+1,r,s+"("); dfs(res,n,l,r+1,s+")"); } }; |
相关文章推荐
- 263. Ugly Number
- DoTween小结
- Java计算程序运行时间
- 实现ListView的弹性效果,下拉隐藏ToolBar(ActionBar),上拉显示ToolBar
- HttpClient使用详解
- 云计算+大数据+深度学习+人工智能+量子计算=?
- Android-BaseLine框架初识之如何使用(二)
- Atom设置震撼的编辑效果
- 判断线段相交,凸包
- Windbg SOS and CLR版本不一致的解决方案
- 获取中文字符长度遇到的问题,以及解决方法
- PHP函数参数的声明与返回值
- AutoLayout 自动布局
- 算法时间复杂度分析
- GridView & ListView (Android盒子开发)
- Delphi 让自己的软件实现双击打开文件 转
- Computer Vision and Machine Learning Competitions
- spring mvc 全局异常处理
- Linux
- IOS runtime运行时