题意

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)的更多相关文章

  1. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  2. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  3. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  4. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  5. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. LeetCode: Generate Parentheses [021]

    [称号] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  7. LeetCode——Generate Parentheses

    Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...

  8. leetcode Generate Parentheses python

    # 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...

  9. 并没有看起来那么简单leetcode Generate Parentheses

    问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...

随机推荐

  1. 用于创建和管理 Azure 虚拟机的常用 PowerShell 命令

    本文介绍一些可用于在 Azure 订阅中创建和管理虚拟机的 Azure PowerShell 命令. 若要获取特定命令行开关和选项的详细帮助,可以使用 Get-Help 命令. 有关安装最新版 Azu ...

  2. webservice安全性浅谈

    原文地址:http://www.cnblogs.com/chhuic/archive/2009/11/19/1606109.html 做项目时,经常会用到WebService来通讯,但WebServi ...

  3. python设计模式之门面模式

    一.结构型设计模式 门面模式与单例模式,工厂模式不同,它是一种结构型模式. 结构型模式描述如何将对象和类组合成更大的结构 结构型模式是一种能够简化设计工作的模式,它能找出更简单的方法来认识或表示实体之 ...

  4. PHP类继承、接口继承关系概述

    PHP类继承: PHP类不支持多继承,也就是子类只能继承一个父类,但是支持多层次继承,比如: class frist{ public function __construct(){ echo &quo ...

  5. javascript 正则表达式的使用

    1. 语法 有两种定义正则表达式的方式 字面量形式 var expression = /pattern/flags 引用 MDN 的解释: pattern:正则表达式的文本. flags:标志,可以是 ...

  6. SQLite简单使用记录

    SQLite,一种轻量级的数据库 想要使用的话首先下载安装包. https://www.sqlite.org/download.html 下载sqlite-netFx20-setup-bundle-x ...

  7. c++ 远程连接局域网内 数据库,并进行操作

    首先尝试利用Windows自带的dos命令窗口操作数据库:参考见https://jingyan.baidu.com/article/3aed632e19b5e8701080918f.html 1.搜索 ...

  8. 面向对象程序设计_Task7_Summary

    Summary of the ... 题目链接:第七次作业 终于还是迎来了这学期的最后一次作业,唠叨话还是放最后说,先说说计算器这玩意儿吧 贯穿了整个学期的计算器,要是让我对自己做个评价,顶多只是还好 ...

  9. SQL server安装连接

    原文:https://blog.csdn.net/andrewniu/article/details/78485312 原文:https://jingyan.baidu.com/article/76a ...

  10. jquery attr处理checkbox / select 等表单元素时只能使用一次的坑

    先上html结构 <body> <form action=""> <input type="checkbox" id=" ...