【leetcode】22. Generate Parentheses
2015-11-03 19:49
309 查看
dfs,只要 ( 的数量小于 ) 的数量,即可以不断的dfs
/** * @author johnsondu * @problem Generate Parentheses * @url https://leetcode.com/problems/generate-parentheses/ * @strategy dfs, only if left "(" less than right ")", then it is legal. * @status Accepted, runtime beats 34.01% of cpp submissions. 0ms * @time 19:46, Nov 3th 2015 */ class Solution { public: void dfs(string tmp, int lf, int rt, vector<string>& ans) { if(lf == 0 && rt == 0) { ans.push_back(tmp); } if(lf != 0 && lf <= rt) { dfs(tmp + "(", lf - 1, rt, ans); } if(lf < rt) { dfs(tmp + ")", lf, rt - 1, ans); } } vector<string> generateParenthesis(int n) { vector<string> ans; if(n == 0) return ans; dfs("(", n-1, n, ans); return ans; } };
相关文章推荐
- 如何修改Android应用包名
- artTemplate 自动化编译之tmod
- 【noip冲刺赛】:循环整数
- 64位CentOS6.5下安装hive
- Inna and New Matrix of Candies(CodeForces - 400B)
- lightoj 1224(trie)
- 醉醒间
- How GPUs Work
- linux kernel log 级别设置
- php字符串处理函数总结
- Oracle的基本操作
- 远程链接oracle-12519
- Adaboost与boosting
- Linux下安装JDK
- 【LeetCode OJ】Longest Substring Without Repeating Characters
- 表情匹配总数 DP SRM 671 div1 300 BearCries
- Android实战技巧:如何在ScrollView中嵌套ListView
- [GitHub]简单几步将你的项目托管到GitHub
- 基于RHCS+iSCSI+CLVM实现Web服务的共享存储集群架构
- Android测试 ---- Espresso + Jacoco