22.Generate Parentheses[M]括号生成
题目
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\)对有效括号的生成,我们可以将其看成以下的方式:

图1:回溯法生成括号示意图
在上图中,由于一对有效括号总是从"("开始,所以树的根节点是"("。将左括号的个数记为$l$,右括号的个数记为$r$,给定个数$n$,在生成新括号的过程中,分为三种情况
* $l r$,说明右括号的个数不够多,应该生成右括号
* $r = n$,说明完成$n$对有效括号的生成。
注意在此过程中,右括号的个数不能超过左括号,如果超过,则不往下进行递归。由此完成了一个回溯法的过程:递归生成括号,但是在生成括号的同时,检查左右括号是否匹配。如果匹配,则继续递归;如果不匹配,则不往下递归。在具体实现中,通过保证右边括号的个数\(r\)始终小于等于左边括号的个数来实现匹配的检查。
Tips
回溯法
基本思想
将问题的解空间转化为图或者树的结构表示,然后利用深度优先搜索策略进行遍历,遍历过程中记录和寻找可行解和最优解。
基本行为
回溯法的基本行为是搜索,在搜索过程中利用两种方法来避免无效的搜索
- 1.使用约束函数,剪去不满足约束条件的路径
- 2.使用限定条件,剪去不能得到最优解的路径
回溯法是一种思想方法,在具体实现中是通过递归或者迭代实现。
C++
vector<string> generateParenthesis(int n) {
vector<string> result;
if(n==0)
return result;
backTrack(result, "", 0, 0, n);
return result;
}
void backTrack(vector<string> &res,string curStr,int l, int r, int n){
if(r == n)
res.push_back(curStr);
//如果左括号没有达到给定的n
if(l < n)
backTrack(res, curStr+"(", l+1, r, n);
//如果右括号数目不够
if(r < l)
backTrack(res, curStr+")", l, r+1, n);
}
Python
参考
[1]
[2] https://blog.csdn.net/zjc_game_coder/article/details/78520742
22.Generate Parentheses[M]括号生成的更多相关文章
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- Linux scp 后台运行传输文件
Linux scp 设置nohup后台运行 1.正常执行scp命令 2.输入ctrl + z 暂停任务 3.bg将其放入后台 4.disown -h 将这个作业忽略HUP信号 5.测试会话中断,任务继 ...
- 分布式机器学习框架:MxNet 前言
原文连接:MxNet和Caffe之间有什么优缺点一.前言: Minerva: 高效灵活的并行深度学习引擎 不同于cxxnet追求极致速度和易用性,Minerva则提供了一个高效灵活的平台 ...
- php多进程防止出现僵尸进程
对于用PHP进行多进程并发编程,不可避免要遇到僵尸进程的问题. 僵尸进程是指的父进程已经退出,而该进程dead之后没有进程接受,就成为僵尸进程(zombie)进程.任何进程在退出前(使用exit退出) ...
- Day 23 类的继承,派生,组合,菱形继承,多态与多态性
类的继承 继承是一种新建类的方式,新建的类称为子类,被继承的类称为父类 继承的特性是:子类会遗传父类的属性 继承是类与类之间的关系 为什么用继承 使用继承可以减少代码的冗余 对象的继承 python中 ...
- 分治FFT模板
题目链接:https://www.luogu.org/problemnew/show/P4721 总结了一下蒟蒻FFT/NTT容易写错的地方: 1.rev数组求错. 2.cdq注意顺序:先递归 ...
- let、var、const用法区别
1.var var 声明的变量为全局变量,并会进行变量提升:也可以只声明变量而不进行赋值,输出为undefined,以下写法都是合法的. var a var a = 123 2.let let 声明 ...
- [luogu1640 SCOI2010]连续攻击游戏 (二分图最大匹配)
传送门 Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某 ...
- docker安装部署
1. 如何安装 Epel源到 RHEL/CentOS 7/6/5? RHEL/CentOS rpm -ivh http://mirrors.ustc.edu.cn/epel/7/x86_64/Pack ...
- 26.bulk批量操作
主要知识点 1.bulk语法 2.bulk使用时的注意事项 3.bulk size 对es性能的影响 一.bulk语法 每一个操作要两个json串(delete操作除外),每个json串占一行 ...
- kissui.scrollanim页面滚动动画库插件
简介 kissui.scrollanim是一款实用的纯JS和CSS3页面滚动动画库插件.通过该插件可以使元素进入浏览器视口的时候,展示指定的CSS3动画效果. 下载地址及演示 在线演示 在线下载 安装 ...