题目

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\)对有效括号的生成,我们可以将其看成以下的方式:

![](https://i.loli.net/2019/05/28/5ced1c673eb7529167.jpg)
图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]括号生成的更多相关文章

  1. LeetCode OJ:Generate Parentheses(括号生成)

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

  2. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  3. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  4. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

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

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

  6. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  7. 22. Generate Parentheses (recursion algorithm)

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

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

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

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

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

随机推荐

  1. Lazarus开发环境编译选项配置

    Lazarus的环境配置让人有点犯晕,对于刚从delphi转到lazarus上的我来说,每次新建工程都会遇到一堆Can't find unit xxxx used by xxxx的问题,问题虽然不大, ...

  2. 【sqli-labs】 less21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入)

    这个和less20是一样的,唯一的不同在于添加了括号和使用了base64对cookie进行了编码(因为使用了base64_decode解码函数) admin被编码成了YWRtaW4=但是执行的SQL语 ...

  3. 身份认证防止重放攻击的challenge-response方法

    或者叫询问-应答机制. 基于挑战/应答(Challenge/Response)方式的身份认证系统就是每次认证时认证服务器端都给客户端发送一个不同的"挑战"字串,客户端程序收到这个& ...

  4. Apex语言(四)选择(决策)结构

    1.选择结构 选择结构是当满足某个条件或不满足某个条件时,需要进行决策以控制执行的流程. 2.if语句 if语句由布尔表达式后跟一个或多个语句组成. [格式] if(条件表达式){ 语句: } [流程 ...

  5. webstorm前端开发工具vue环境配置及运行项目

    1:webstorm的安装:2:node.js的安装3:安装Git4:vue-cli 安装前面两步就可以把项目启动了,安装Git主要是打开命令窗口,这样就可以用liunx命令了,原理跟cmd差不多 V ...

  6. 解决HTML select控件 设置属性 disabled 后无法向后台传值的方法

    大家都知道有时候修改数据的时候我们希望有一些数据是不可以修改的,通常情况下我们会将input框设置为 readonly , 但是 select 控件没有这个属性,需要使用另一个属性 disabled ...

  7. python第九周:paramiko多线程、队列

    1.paramiko模块 用处:连接远程服务器并执行相关操作 使用方法: SSHClient:连接远程服务器并执行基本命令 import paramiko #创建SSH对象 ssh = paramik ...

  8. linux根据进程名字杀死进程

    ps -ef | grep procedure_name | grep -v grep | awk '{print $2}' | xargs kill -9 Linux Shell脚本实现根据进程名杀 ...

  9. UOJ #214 合唱队形 (概率期望计数、DP、Min-Max容斥)

    9个月的心头大恨终于切掉了!!!! 非常好的一道题,不知为何uoj上被点了70个差评. 题目链接: http://uoj.ac/problem/214 题目大意: 请自行阅读. 题解: 官方题解讲得相 ...

  10. 译:滑雪租赁问题(ski rental problem)

         本文翻译自维基百科词条:http://en.wikipedia.org/wiki/Ski_rental_problem 滑雪租赁问题(ski rental problem)是一类问题的总称, ...