LeetCode Generate Parentheses (DFS)
题意
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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
输出N对括号的所有组合。
解法
比较明显的深搜,主要保存两个变量,一个left用来记录已经安放的左括号的数量,相当于一个栈,还有一个avaliable用来记录剩下还有多少个左括号可以取。当left不为空时,对于每一次新括号的选取,可以新增一个左括号(如果avaliable还没空的话),或者是取一个右括号来和之前的一个左括号匹配。当left为空时,能做的就只有一直取右括号来和前面的左括号匹配。
class Solution
{
public:
vector<string> generateParenthesis(int n)
{
vector<string> ans;
string temp;
dfs(0,n,0,n,ans,temp);
return ans;
}
void dfs(int left,int avaliable,int length,int n,vector<string> & ans,string temp)
{
if(length == n * 2)
{
ans.push_back(temp);
return ;
}
if(left)
{
if(avaliable)
{
temp += '(';
dfs(left + 1,avaliable - 1,length + 1,n,ans,temp);
temp.pop_back();
}
temp += ')';
dfs(left - 1,avaliable,length + 1,n,ans,temp);
temp.pop_back();
}
else
if(avaliable)
{
temp += '(';
dfs(left + 1,avaliable - 1,length + 1,n,ans,temp);
temp.pop_back();
}
}
};
LeetCode Generate Parentheses (DFS)的更多相关文章
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- LeetCode Generate Parentheses 构造括号串(DFS简单题)
题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode: Generate Parentheses [021]
[称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode——Generate Parentheses
Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...
- leetcode Generate Parentheses python
# 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
随机推荐
- 超经典sql练习题,在teradata上实现
题目来源:https://blog.csdn.net/flycat296/article/details/63681089 teradata实现: drop table student; create ...
- UNIX高级环境编程(12)进程关联(Process Relationships)- 终端登录过程 ,进程组,Session
在前面的章节我们了解到,进程之间是有关联的: 每个进程都有一个父进程: 子进程退出时,父进程可以感知并且获取子进程的退出状态. 本章我们将了解: 进程组的更多细节: sessions的内容: logi ...
- DevExpress08、SchedulerControl、DateNavigator、SpreadsheetControl
SchedulerControl 该控件以可视化的效果显示预约或者设定的行程: 该控件预约后的数据存储在SchedulerStorage对象里, 当以拖动形式添加SchedulerControl控件到 ...
- 阿里八八Alpha阶段Scrum(9/12)
今日进度 叶文滔: 成功完成多级悬浮按钮,并添加与日程输入界面的连接.debug了一些对接产生的问题 黄梅玲: 合并单日项目至多日.获取json数据 王国超: 完成了初步的多日界面,pull至项目.进 ...
- 一些node模块的学习思考
12月14日清单 1 readline模块 var readline = require("readline"); // input 是必须的,output是可选的 rl = re ...
- go变量和常量
一.变量 1. 命名规则:字母或者_下划线开头 2.“:=” 这种模式只能用于函数内部,常量const不能用这种模式来定义 二.常量 1. 常量const必须在定义的时候就赋值 2. 常量的值在整个过 ...
- PHP缓存锁原理及利用
原文链接:https://blog.csdn.net/tim_phper/article/details/54949404 概述: 项目当中经常要考虑数据高并发的情况,为了避免并发导致出现一些资源重复 ...
- oracle 更新用户密码,授连接权限,
1.授连接权限 grant connect to 用户名; ALTER USER 用户名 ACCOUNT UNLOCK; 2.更新密码 ALTER USER 用户名 IDENTIFIED BY 更新密 ...
- 筛选法求N以内的所有素数
素数:一个数只能被1和它本身整除的数.2是最小的素数 #include <iostream> using namespace std; #define NUM 100 ]; int mai ...
- linux固定ip地址
最近自己搭jenkins发现ifconfig出来ip老是变来变去决定固定服务ip,原来配置: [root@bogon bin]# cat /etc/sysconfig/network-scripts/ ...