给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 n = 3,生成结果为:

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

class Solution {
public:
vector<string> res;
vector<string> generateParenthesis(int n)
{
if(n == 0)
return res;
DFS("", n, n);
return res;
} //必须满足right >= left
void DFS(string str, int left, int right)
{
if(left == 0 && right == 0)
{
res.push_back(str);
return ;
}
char c = 0;
if(left == right)
{
c = '(';
DFS(str + c, left - 1, right);
}
else
{
for(int i = 0; i < 2; i++)
{
if(i == 0 && left > 0)
{
c = '(';
DFS(str + c, left - 1, right);
}
else if(i == 1 && right > 0)
{
c = ')';
DFS(str + c, left, right - 1);
}
}
}
}
};

Leetcode22.Generate Parentheses括号生成的更多相关文章

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

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

  2. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

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

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

  4. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

  5. LeetCode22 Generate Parentheses

    题意: iven n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

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

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

  7. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  8. generate parentheses(生成括号)

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

  9. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

随机推荐

  1. redis深入学习(一)-----CAP、redis数据类型

    NoSQL数据库的四大分类 KV键值: memcache+redis 文档型数据库(bson格式比较多): MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在 ...

  2. GC 案例收集整理

    1.数组动态扩容  现象:系统一直在做cms gc,但是老生代一直不降下去,但是执行一次jmap -histo:live之后,也就是主动触发一次full gc之后,通过jstat -gcutil来看老 ...

  3. STL与泛型编程-练习2-GeekBand

    练习题目: struct Programmer{ Programmer(const int id, const std::wstring name): Id(id), Name(name){ } vo ...

  4. Spring框架中的核心思想包括什么

    (1)依赖注入 (2)控制反转 (3)面向切面

  5. Django项目:CMDB(服务器硬件资产自动采集系统)--07--06CMDB测试Linux系统采集硬件数据的命令02

    #settings.py """ Django settings for AutoCmdb project. Generated by 'django-admin sta ...

  6. 吴恩达《机器学习》课程总结(18)_照片OCR

    18.1问题描述和流程图 (1)图像文字识别是从给定的一张图片中识别文字. (2)流程包括: 1.文字侦测 2.字符切分(现在不需要切分了) 3.字符分类 18.2滑动窗口 在行人检测中,滑动窗口是首 ...

  7. sql调优的总结

    sql调优的总结 列类型尽量定义成数值类型,且长度尽可能短,如主键和外键,类型字段等等 建立单列索引 根据需要建立多列联合索引 当单个列过滤之后还有很多数据,那么索引的效率将会比较低,即列的区分度较低 ...

  8. 软件-浏览器-GoogleChrome:Google Chrome

    ylbtech-软件-浏览器-GoogleChrome:Google Chrome Google Chrome是一款由Google公司开发的网页浏览器,该浏览器基于其他开源软件撰写,包括WebKit, ...

  9. jmeter体系结构

    jmeter体系结构 jmeter体系结构: 1.取样器.断言.监听器组合在一起就可以帮助我们完成发送请求.验证结果及记录结果三项工作 (1)取样器的访问路径:[测试计划]---[线程组]       ...

  10. css3之弹性盒模型(Flex Box)

    CSS3 弹性盒子(Flex Box) 弹性盒子是 CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时 ...