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 ...
随机推荐
- Deutsch lernen (09)
1. die Rückmeldung, -en 反馈,回馈:(销假)报到 die Rückmeldung zur Arbeit 2. dringend a. 紧急的,急切的 Ich brauche d ...
- jq 替换DOM layeui 不刷新
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CodeIgniter-CI之MySQL
首先我们需要进行一下配置,这里需要修改的文件为application目录下的config目录下的database.php文件,我们修改相应的配置项,比如这里是我的配置情况: 通常我们在操作数据库之前, ...
- 企业版 Linux 附加软件包(EPEL)
企业版 Linux 附加软件包(以下简称 EPEL)是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientific Linux ...
- Centos7下的有多个版本的tomcat部署项目并访问
在tomcat下部署项目,第一次访问成功.但是发现有一个页面没有成功加载,发现是部署时少了一些东西.也不想去找出具体少了什么,直接把原来的项目移除,重新加载项目.重启tomcat,这时惊喜来了. 重启 ...
- Java代码规范和一些常见问题
本文中的代码规范,是Java标准代码规范中的一小部分,在我看来,是最重要的一部分. 理想目标:不需要写注释,不需要和别人介绍,别人就知道你的项目大致是做什么的,每个类大概实现了什么功能. ...
- python数据标准化
def datastandard(): from sklearn import preprocessing import numpy as np x = np.array([ [ 1., -1., 2 ...
- 手把手实现Java权限(1)-Shiro介绍
功能介绍 Authentication :身份认证/登录.验证用户是不是拥有对应的身份: Authorization :授权,即权限验证.验证某个已认证的用户是否拥有某个权限:即推断用 户能否做事 ...
- 几种new
http://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html new .operator new 和 placement new 区 ...
- Unix(AIX) set命令
Unix(AIX) set命令,set命令是shell所具有的,不仅仅是aix自己有的: set -o vi 可以用vi方式操作,用来获取已经输入过的命令: 如果希望自动设置,可以在.profile ...