题面

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses 
给定int n,代表n组括号,编码生成所有有效的括号组合(即符合括号嵌套规则)

 样例

given n = 3, a solution set is: 

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

思路

dfs

源码

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> res;
dfs("", , , res, n);
return res;
}
//dfs深搜
void dfs(string tmp, int l, int r, vector<string> &res, int n)
{
if(l == n && r == n)
{
res.push_back(tmp);
return ;
}
if(l < n)
dfs(tmp+"(", l+, r, res, n);
if(l > r)
dfs(tmp+")", l, r+, res, n);
}
};

dfs · leetcode-22.产生括号组?的更多相关文章

  1. Leetcode 22.生成括号对数

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

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

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

  3. leetcode 22. 括号生成 dfs

    先思考符合要求的串是什么样子的 任意时刻,(数量大于),且最后(==)==n即可 考虑下一个加入string的字符时(或者)即可 dfs class Solution { public: vector ...

  4. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  5. Java实现 LeetCode 22 括号生成

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

  6. leetcode 22括号生成

    非常好的一道题.一开始的思想是这样的,先把n对括号按照某一顺序生成一个string,然后用全排列算法生成所有可能,然后利用stack写一段判断括号是否匹配的字符串,匹配的假如结果中.不过会超时.因为全 ...

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

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

  8. [LeetCode] 22. 括号生成(回溯/DP)

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

  9. Leetcode(22)-括号生成

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

随机推荐

  1. java如何获取项目的工作目录

    package maptoxml; public class Tfff { public static void main(String[] args) { System.out.println(&q ...

  2. 小程序scroll-view 使用

    <scroll-view class="box" scroll-x="true" > <view </view> <view ...

  3. UIwindow ---密码框

    程序运行显示结果如下 : 验证密码输入错误显示如下: 代码如下 : 1> ////  PasswordInputWindow.m//  UIWindow--密码框////  Created by ...

  4. 【Leetcode_easy】824. Goat Latin

    problem 824. Goat Latin solution class Solution { public: string toGoatLatin(string S) { unordered_s ...

  5. django model的update时auto_now不被更新的原因

    gmt_create自动添加auto_now_add:gmt_modify自动更新auto_now class CommonInfo(models.Model): """ ...

  6. (CVE-2015-0240)Samba远程代码执行

    简介 Samba 是利用 SMB 协议实现文件共享的一款著名开源工具套件.日前 Samba 曝出一个严重安全漏洞,该漏洞出现在 smbd 文件服务端,漏洞编号为 CVE-2015-0240,可以允许攻 ...

  7. MySQL5.7.9安装与配置优化

    一. 环境准备 1. 下载软件包 wget http://test.hexin.cn/software/mysql-5.7.9.tar.gz -P /usr/local/src/ wget http: ...

  8. Java面试 - final、finally、finalize的区别?

    final:用于声明属性, 方法和类,分别表示属性不可变.方法不可覆盖.被其修饰的类不可继承. finally:异常处理语句结构的一部分,表示总是执行. finalize:Object 类的一个方法, ...

  9. 深度图转伪彩色图(python)

    kinect得到的深度图灰不拉几,人眼很难识别出其中的物体,感知深度的变化. 在做展示的时候,我们往往需要可视化,使用OpenCV的预定义的颜色映射来将灰度图像伪彩色化,在OpenCV中使用apply ...

  10. Linux "yin"才们的奇"yin"小技巧 --请用东北发音夸他们

    1. include/linux/bits.h GENMASK(h, l) /* * Create a contiguous bitmask starting at bit position @l a ...