LeetCode——022
2016-04-16 11:26
288 查看
/*
22. Generate Parentheses My Submissions QuestionEditorial Solution
Total Accepted: 85070 Total Submissions: 231341 Difficulty: Medium
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:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
Subscribe to see which companies asked this question
*/
/*
解题思路:
利用深度遍历dfs
能够往下走的条件就是左括号数 、右括号数有没达到n值得。在每一步过程中必须保证left>=right,否则返回
*/
class Solution { public: vector<string> generateParenthesis(int n) { //此题用深度遍历即可 vector<string> res; dfs(n,0,0,"",res); return res; } void dfs(int n,int left,int right,string out,vector<string>&res){ if(left==n&& right==n){ res.push_back(out); return ; } if(right>left || left>n )return ; dfs(n,left+1,right,out+'(',res); dfs(n,left,right+1,out+')',res); } };
相关文章推荐
- java之路径分隔符介绍
- 改革春风吹满地 计算几何
- Layer 1: 深入分析Parquet列式存储格式
- POJ 1679 The Unique MST【最小生成树问题相关】
- 演示request的常用方法
- VS 应用模板 所交税和实发工资的运算
- Java线程创建和启动
- 比较好的按键处理方法以及实例
- Android_TextView中部分文字响应事件
- 数据库理论之ACID和BASE的比较
- codevs 1191 数轴染色 区间更新加延迟标记
- 第6周项目4-静态成员应用
- VS 星期作业 if else的应用 做一个受不受异性欢迎的小程序
- Code Forces 22B Bargaining Table
- iOS CocoaPods安装和使用
- java.net.UnknownHostException
- Code Forces 22B Bargaining Table
- Oracle操作
- OpenGL ES像素着色器教程
- 逆波兰表达式(后缀表达式)