题意

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. 深度访谈Amazon员工与HR:华裔因pip跳楼背后(图)

    http://www.wenxuecity.com/news/2016/12/01/5813342.html 首先,让我们来回顾一下这起事件.两天前在某论坛中,有同学发了这么一个帖子,大致意思是说有一 ...

  2. HDFS核心设计

    一.HDFS核心设计 数据块(block) 数据块是HDFS上最基本的存储单位 HDFS块默认大小为128M         对块进行抽象会带来的好处 一个小文件的大小可以大于网络中任意一个磁盘的容量 ...

  3. [技术] OIer的C++标准库 : 字符串库

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...

  4. python3: 数字日期和时间(2)

    12.基本的日期与时间转换 Q: 你需要执行简单的时间转换,比如天到秒,小时到分钟等的转换 A: 为了执行不同时间单位的转换和计算,请使用 datetime 模块. 比如,为了表示一个时间段,可以创建 ...

  5. FastDFS_v5.05+nginx+cache集群安装配置手册

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.FastDFS简单介绍 FastDFS是由淘宝的余庆先生所开发,是一个轻量级.高性能的开源分布式文件系统, ...

  6. 2.HBase In Action 第一章-HBase简介(1.1数据管理系统:快速学习)

    Relational database systems have been around for a few decades and have been hugely successful in so ...

  7. 使用docker-compose运行Django

    1.新建空目录 2.进入该目录新建Dockerfile文件,并在该Dockerfile文件添加如下内容 FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir / ...

  8. Matlab警告消息消除

    在运行matlab程序时候,有些matlab子函数在高版本将会被舍弃,在使用的时候,matlab编译器就会报出警告信息. The COMBNTNS function will be removed i ...

  9. nginx配置收集

    同个服务,分别读取不同借口 location /xibao/service_api/ { if ($request_uri ~ ^/xibao/(.*)) { set $xibao_data_url ...

  10. statefulSet + headless service 学习记录 service :selector --> template :label

    1.statefulset.yaml apiVersion: apps/v1kind: StatefulSetmetadata:   name: webspec:    serviceName: &q ...