LeetCode OJ: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:
"((()))", "(()())", "(())()", "()(())", "()()()"
求出所有可能的括号组合,在dfs加上回溯条件,右括号数目不能超过左括号,还有就是左括号数最大不能超过n,代码如下:
class Solution {
public:
vector<string> generateParenthesis(int n) {
dfs(,n*,,,"");
return ret;
}
void dfs(int dep, int maxDep, int left, int leftTotal, string curr){
if(leftTotal* > maxDep)
return;
if(dep == maxDep){
ret.push_back(curr);
return;
}
for(int i = ; i < ; i++){
if(i == ){
dfs(dep+, maxDep, left+, leftTotal+, curr+"(");
}else{
if(left)
dfs(dep+, maxDep, left-, leftTotal, curr+")");
}
}
}
private:
vector<string> ret;
};
LeetCode OJ:Generate Parentheses(括号生成)的更多相关文章
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- Leetcode22.Generate Parentheses括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- [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 ...
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
随机推荐
- 对Java ConcurrentHashMap的一些了解
①引言(为什么要使用ConcurrentHashMap) 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. Has ...
- windows下mysql安装失败的一个解决案例
操作系统:windows8.1,之前安装过mysql,这次安装在配置的最后一部执行“Apply security settings”的过程中弹出经典错误: Access denied for user ...
- JVM性能调优(转)
本文转自:http://www.cnblogs.com/chen77716/archive/2010/06/26/2130807.html 最近因项目存在内存泄漏,故进行大规模的JVM性能调优 , 现 ...
- JavaScript 事件处理详解
事件绑定与解绑: el.onEventName = function (){} document.getElementById("dom").onclick = function( ...
- ashx 方法模板
; ); //查询字符串拼接 string searchparams = DTRequest.GetQueryString("jsonstring"); fooddetail mo ...
- [caffe]caffe资料收集
1.caffe主页,有各种tutorial. 2.Evan Shelhamer的tutorial,包括视频.
- SQL学习笔记之MySQL查询的三层解析
Mysqld的三层结构: SQL类型: DDL:数据库对象定义语言 对库和表的定义 DML:操作语言 DCL:控制语言 结构化的查询语言:select * from user; 执行该语句时: 1.连 ...
- PHP秒杀系统全方位设计分析(一)
秒杀系统特点人多商品少时间短流量高外挂机器[黄牛和非黄牛] 技术分析瞬间高并发的处理能力多层次的分布式处理能力人机交互与对抗[12306验证码图片] 技术选型分析Linux+Nginx+PHP+Mys ...
- Python3.x:代理ip刷点赞
Python3.x:代理ip刷点赞 声明:仅供为学习材料,不允许用作商业用途: 一,功能: 针对某网站对企业自动刷点赞: 网站:https://best.zhaopin.com/ 二,步骤: 1,获取 ...
- Pycharm安装Python第三方库
转自:http://blog.csdn.net/qiannianguji01/article/details/50397046 除了使用easy_insatll和pip工具安装Python第三方库外还 ...