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 ...
随机推荐
- js拼接table查询信息部分
其一: $("#datagrid").empty(); var a=0; <order-rows> a++; $("#datagrid").appe ...
- MySQL 8.0有什么新功能
https://mysqlserverteam.com/whats-new-in-mysql-8-0-generally-available/ 我们自豪地宣布MySQL 8.0的一般可用性. 现在下载 ...
- 在Ubuntu17.04中遇到无法清空回收站解决方法
在Ubuntu17.04下,遇到清空回收站文件时报错,提示”Failed to delete the item from the trash”,无法清空回收站. 回收站其实就是一个文件夹,存放着被删掉 ...
- 深度访谈Amazon员工与HR:华裔因pip跳楼背后(图)
http://www.wenxuecity.com/news/2016/12/01/5813342.html 首先,让我们来回顾一下这起事件.两天前在某论坛中,有同学发了这么一个帖子,大致意思是说有一 ...
- Android开发中使用七牛云存储进行图片上传下载
Android开发中的图片存储本来就是比较耗时耗地的事情,而使用第三方的七牛云,便可以很好的解决这些后顾之忧,最近我也是在学习七牛的SDK,将使用过程在这记录下来,方便以后使用. 先说一下七牛云的存储 ...
- 1.4环境的准备(四)之Pycharm的使用技巧
返回总目录 目录: 1.快捷键的使用: 2.提示技巧: 3.其他技巧: (一)快捷键的使用: (1)Pycharm自带默认的快捷键 1.Ctrl + C 复制 2.Ctrl + V 粘贴 3.Ctrl ...
- def chi(*food,**kw):
def chi(*food,**kw): print(food,kw)chi("cong","蒜",'姜','可乐',"J=Juice",a ...
- python第三十四课——2.匿名函数配合容器函数的使用
匿名函数配合容器函数的使用(了解) 1.匿名函数配合列表对象使用 lt=[lambda x:x**2,lambda x:x**3,lambda x:x**4] for i in lt: print(i ...
- screen命令使用
screen -S + name:创建一个名字叫做name的会话.在里面执行你想要执行的程序,再用Ctrl+a+d退出,让会话Detached,这样就能保证你的任务在后台一直运行,也不会随着终端的关闭 ...
- linux 邮件工具利器sendEmail时效超好
下载:http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 安装方法: ) Extract the pac ...