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 ...
随机推荐
- MSP430之section(1)
1 Intro The smallest unit of an object file is a section. A section is a block of code or data that ...
- C# 检测dll的新版本号方法
FileVersionInfo info = FileVersionInfo.GetVersionInfo(YourFileNameHere);string version = info.FileMa ...
- 【剑指Offer】27、字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. ...
- HTTP 下载文件中文文件名在 Firefox 下乱码问题
转自:http://www.imhdr.com/991/ HTTP 下载文件,中文文件名在 Firefox 下乱码问题 最近帮助一同事解决 HTTP 下载文件时,中文文件名在 Firefox 下乱码的 ...
- html表单练习(讲义中的方法)
用table作框体固定位置结构 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- [luogu2825 HEOI2016/TJOI2016] 游戏 (二分图最大匹配)
传送门 Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看是否能炸到对手,或者躲开对手的炸弹.在玩游戏的过程中,小H想到了 ...
- php进程daemon化的正确做法
[原文地址:https://blog.ti-node.com/blog...] daemon 音标 : ['di:mən] , 中文含义为守护神或精灵的意思 . 其实它还有个意思 : 守护进程 . 守 ...
- Bootstrap关于表单(二):水平表单
Bootstrap框架默认的表单是垂直显示风格,但很多时候我们需要的水平表单风格(标签居左,表单控件居右) 在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件: 1.在<form ...
- numpy中sum(axis=0)和axis=1的计算原理
看起来挺简单的样子,但是在给sum函数中加入参数.sum(a,axis=0)或者是.sum(axis=1) 就有点不解了 在我实验以后发现 我们平时用的sum应该是默认的axis=0 就是普通的相加 ...
- 使用idea创建maven项目时 需要注意的问题
截几张图来说明吧 上面的红色框如果不选中 将来创建的工程中没有webapp目录以及下面的子目录 选中之后 然后一直“下一步” 直到工程创建 但是我第一次按照正确的方式操作时 工程中并没有src ...