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:

"((()))", "(()())", "(())()", "()(())", "()()()"

求出所有可能的括号组合,在dfs加上回溯条件,右括号数目不能超过左括号,还有就是左括号数最大不能超过n,代码如下:

 class Solution {
public:
vector<string> generateParenthesis(int n) {
dfs(,n*,,,"");
return ret;
} void dfs(int dep, int maxDep, int left, int leftTotal, string curr){
if(leftTotal* > maxDep)
return;
if(dep == maxDep){
ret.push_back(curr);
return;
}
for(int i = ; i < ; i++){
if(i == ){
dfs(dep+, maxDep, left+, leftTotal+, curr+"(");
}else{
if(left)
dfs(dep+, maxDep, left-, leftTotal, curr+")");
}
}
}
private:
vector<string> ret;
};

LeetCode OJ:Generate Parentheses(括号生成)的更多相关文章

  1. [LeetCode]22. Generate Parentheses括号生成

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

  2. 【LeetCode】22. Generate Parentheses 括号生成

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...

  3. Leetcode22.Generate Parentheses括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  4. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  5. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  6. [LeetCode] 22. Generate Parentheses 生成括号

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

  7. [leetcode]22. Generate Parentheses生成括号

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

  8. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

  9. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

  10. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

随机推荐

  1. IOS开发-数据库总结

    关于数据存储概念: 数据结构: 基本对象:NSDictionary.NSArray和NSSet这些对象. 复杂对象:关系模型.对象图和属性列表多种结构等. 存储方式: 内存:内存存储是临时的,运行时有 ...

  2. JAVA BIO与NIO、AIO的区别

    IO的方式通常分为几种,同步阻塞的BIO.同步非阻塞的NIO.异步非阻塞的AIO. 一.BIO 在JDK1.4出来之前,我们建立网络连接的时候采用BIO模式,需要先在服务端启动一个ServerSock ...

  3. IPMI的几个问题

    IPMI针对大量监控.控制和自动回复服务器的作业,提供了智能型的管理方式.此标准适用于不同的服务器拓朴学,以及Windows.Linux. Solaris.Mac或是混合型的操作系统.此外,由于IPM ...

  4. get请求参数为中文,参数到后台出现乱码(注:乱码情况千奇百怪,这里贴我遇到的情况)

    前言 get请求的接口从页面到controller类出现了乱码. 解决 参数乱码: String param = "..."; 使用new String(param.getByte ...

  5. Python面试题之Python中type和object的关系

    知乎上看到的提问: 两个是互为实例的关系,但不是互为子类的关系,只有type是object的子类,反之则不成立. 大牛说两者是蛋生鸡鸡生蛋的关系,但我还是不明白,有懂的麻烦解释一下, 希望不要给出外文 ...

  6. Django---ModelForm详解

    示例: from django.db import models from django.forms import ModelForm TITLE_CHOICES = ( ('MR', 'Mr.'), ...

  7. RocEDU.阅读.写作《乌合之众》(二)

    第二卷 群体的意见与信念 决定着群体意见与信念的因素分为两类:直接因素与间接因素. 直接因素:使观念采取一定形式并且使它能够产生一定结果的因素. 间接因素:能够使群体接受某些信念并使其难以接受别的信念 ...

  8. 内核启动时在挂载ubi文件系统时提示UBIFS error (ubi0:0 pid 1): ubifs_read_superblock: min. I/O unit mismatch

    一.背景 1.1 笔者机器的内核错误信息如下: UBIFS error (ubi0:0 pid 1): ubifs_read_superblock: min. I/O unit mismatch: 2 ...

  9. Swift学习笔记 - URL编码encode与解码decode

    使用swift有一段时间了,api的变换造成了很多困扰,下面是关于url编码和解码问题的解决方案 在Swift中URL编码 在Swift中URL编码用到的是String的方法 func addingP ...

  10. spark SQL学习(综合案例-日志分析)

    日志分析 scala> import org.apache.spark.sql.types._ scala> import org.apache.spark.sql.Row scala&g ...