给出 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. QQ邮箱发送信息

    #以下库为python自带的库,不需要进行安装 #邮件发信动作 import smtplib #构造邮件内容 from email.mime.text import MIMEText #构造邮件头 f ...

  2. VS 断点不会命中的情况

    总结下遇到的几次断点无法命中的情况: 1.手误设置为release模式 如果是release模式的情况下,断点跳转命中情况是无法预知的,所以请修改成debug 2.与源文件不一致 这个情况是最常见的, ...

  3. 《DSP using MATLAB》Problem 8.31

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  4. python3-常用模块之time

    import time time模块主要是处理各种类型的时间 常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行,单位为秒. 2.time.time() 获取当前时间戳 时间戳 ...

  5. Python爬虫笔记【一】模拟用户访问之设置请求头 (1)

    学习的课本为<python网络数据采集>,大部分代码来此此书. 网络爬虫爬取数据首先就是要有爬取的权限,没有爬取的权限再好的代码也不能运行.所以首先要伪装自己的爬虫,让爬虫不像爬虫而是像人 ...

  6. 如何妥善处理WebBrowser对Javascript的错误问题,阻止JS弹出框,提高用户体验

    由于项目需求,最近转战客户端,开始搞浏览器开发.众所周知,现在在微软平台上开发浏览器,最常用的方法就是扩展Webbrowser,但是首先要清楚的是,WebBrowser控件仅仅是对WebBrowser ...

  7. Ajax请求Session超时解决

    web前端js代码: $.ajaxSetup({ contentType : "application/x-www-form-urlencoded;charset=utf-8", ...

  8. window环境下Python+OpenCV配置

    最近开始学习OpenCV来进行计算机视觉实验,选择了Python作为实验语言,工欲善其事,必先利其器.先总结下安装配置. 现在opencv目测只支持Python2.7X版本的,还依赖于numpy和ma ...

  9. mysql-connector-java-8.0.12使用时报错

    配置url加 &useSSL=false&serverTimezone=UTC 就可以了

  10. Python爬虫工程师必学——App数据抓取实战

    Python爬虫工程师必学 App数据抓取实战 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大 ...