LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
分析:
递归
代码如下:
class Solution {
private:
vector<string> ret;
public:
void solve(int dep, int maxDep, int leftNum, int leftNumTotal, string s)
{
//如果(个数超出,return
if (leftNumTotal * 2 > maxDep)
return;
//如果长度足够,return
if (dep == maxDep){
ret.push_back(s);
return;
}
for(int i = 0; i < 2; i++)
//加 (
if (i == 0)
solve(dep + 1, maxDep, leftNum + 1, leftNumTotal + 1, s + '(');
//加 )
else if (leftNum > 0)
solve(dep + 1, maxDep, leftNum - 1, leftNumTotal, s + ')');
}
vector<string> generateParenthesis(int n){
ret.clear();
solve(0, 2 * n, 0, 0, "");
return ret;
}
};
Java:
public List<String> generateParenthesis(int n) {
ArrayList<String> result = new ArrayList<String>();
dfs(result, "", n, n);
return result;
}
public void dfs(ArrayList<String> result, String s, int left, int right) {
if (left > right) {
return;
}
if (left == 0 && right == 0) {
result.add(s);
return;
}
if (left > 0) {
dfs(result, s + "(", left - 1, right);
}
if (right > 0) {
dfs(result, s + ")", left, right - 1);
}
}
LeetCode 022 Generate Parentheses的更多相关文章
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode之 Generate Parentheses
题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...
随机推荐
- 「APIO2015」巴邻旁之桥 Palembang Bridges
贪心 先转化一下题意 首先如果一个人的家和办公室在河同一侧那么建桥的时候不用去考虑它,最终把答案加上即可 在河两侧的家和办公室互换不影响答案,那么可以把这个抽象到一个区间$[l,r]$,距离就是$|l ...
- How to refresh datasource args caller[X++]
To refresh datasource args caller, you must add override method close on form like source code belo ...
- vuex和axios的基本操作
1.在src目录下创建一个api 是用于集中处理axios的相关配置 index.js就是处理axios的文件 具体如何使用axios 还请百度axios 2.URLs.js是存放需要请求的地址的 3 ...
- 解决git push出现error: failed to push some refs to 错误
错误截图 背景 码云上创建了空项目 本地项目绑定了远程仓库,尝试git push,然后报了错 解决办法 使用强制命令git pull origin master --allow-unrelated-h ...
- ERP中HR模块的操作与设计--开源软件诞生26
赤龙ERP的EHR功能讲解--第26篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/red ...
- 为什么使用MongoDB
MongoDB vs MySQL Nosql vs RDBMS(关系型数据库) MongoDB采用类似Json的形式存储数据而不是结构性的表 MongoDB的分片机制支持海量数据的存储和扩展,并有完整 ...
- S5830 android 2.3.4和2.3.7
12年元旦买的手机S5830,原机自带2.3.4的系统. 看到人家的机子2.3.6的效果稍微绚一点,动了想刷机的念头. 前两天刷了2.3.7,效果还满意,用的还舒服,感觉就是有些费电, 本来就对智能手 ...
- 基于CPU版本的Caffe推理框架
最近一段时间,认真研究了一下caffe.但是,里面内容过多,集合了CPU版本和GPU版本的代码,导致阅读起来有些复杂.因此,特意对caffe代码进行了重构,搭建一个基于CPU版本的Caffe推理框架. ...
- Kafka 生产者分区策略
分区策略 1)分区的原因 (1)方便在集群中扩展,每个 Partition 可以通过调整以适应它所在的机器,而一个 topic 又可以有多个 Partition 组成,因此整个集群就可以适应任意大小的 ...
- 医学AI论文解读 |Circulation|2018| 超声心动图的全自动检测在临床上的应用
文章来自微信公众号:机器学习炼丹术.号主炼丹兄WX:cyx645016617.文章有问题或者想交流的话欢迎- 参考目录: @ 目录 0 论文 1 概述 2 pipeline 3 技术细节 3.1 预处 ...