题意

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. SQL Server中的高可用性(3)----复制 (转载)

    在本系列文章的前两篇对高可用性的意义和单实例下的高可用性做了阐述.但是当随着数据量的增长,以及对RTO和RPO要求的严格,单实例已经无法满足HA/DR方面的要求,因此需要做多实例的高可用性.本文着重对 ...

  2. 在SQL Server中使用CLR调用.NET类库中的方法 (转载)

    在SQL Server中调用 .NET 类库的方法要分为下面几步来实现: 在.NET中新建一个类库项目,并在这个项目中添加一个类文件,并把要被SQL Server调用的方法定义为公有的,静态的方法. ...

  3. InfoPath读取List到重复表

    标题设置好了 添加一个按钮 更改ID  点击编写代码 然后添加引用 并更改下域的名字 添加如下代码 public void LoadBtn_Clicked(object sender, Clicked ...

  4. C Programming vs. Java Programming

    Thing C Java type of language function oriented object oriented basic programming unit function clas ...

  5. tp5 migrate数据库迁移工具

    tp5相对与tp3.2有很大的不同 migrate是其中一点,通过migrate程序员可以在php代码中创建数据库修改回滚等操作 首先下载migrate扩展,命令行到当前项目目录下执行 compose ...

  6. ES6+转ES5

    npm init //创建package.json文件 下载转换babel库及其100+依赖 npm install babel-cli -D npm install babel-preset-env ...

  7. 阿里八八Alpha阶段Scrum(2/12)

    今日进度 叶文滔: 11.1:搭建Andriod Studio开发环境 11.2:已经完成Alpha阶段的APP整体框架搭建. 11.3:根据会议讨论内容,增加了模块标题栏返回键. 王国超: 完成了多 ...

  8. 2.3.1 TextView(文本框)详解

    http://www.runoob.com/w3cnote/android-tutorial-textview.html 1.基础属性详解: 通过下面这个简单的界面,我们来了解几个最基本的属性: 布局 ...

  9. 洛谷P4053 [JSOI2007]建筑抢修

    放题解 题目传送门 放代码 #include <bits/stdc++.h>//万能头 #define MAXN 150000//最多的建筑数量(数据范围) using namespace ...

  10. vSphere虚拟化管理平台的功能

    VMware vSphere® 提供全球领先的虚拟化平台,用于构建云计算基础架构. vSphere 有哪些功能 强大的计算能力 虚拟化 x86 服务器资源并将其聚合成逻辑池,用于分配多个工作负载. 网 ...