博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode] Generate Parentheses
阅读量:7203 次
发布时间:2019-06-29

本文共 777 字,大约阅读时间需要 2 分钟。

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:

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

递归与深搜。

1 class Solution { 2 public: 3     void getRes(vector
&res, int st, string str, int count, int n) { 4 if (str.length() > 2 * n || st > n) return; 5 if (count == n && st == 0) { 6 res.push_back(str); 7 return; 8 } 9 getRes(res, st + 1, str + '(', count, n);10 if (st > 0) getRes(res, st - 1, str + ')', count + 1, n);11 }12 13 vector
generateParenthesis(int n) {14 vector
res;15 getRes(res, 0, "", 0, n);16 return res;17 }18 };

 

转载地址:http://sbzum.baihongyu.com/

你可能感兴趣的文章
Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)...
查看>>
crate安装使用
查看>>
布隆过滤器
查看>>
cocos2dx的对象的内存释放
查看>>
模板特化疑问
查看>>
李京:中国科技大学移动平台——掌上科大
查看>>
<转>Windows下用xcode开发swift程序的图文教程 <一>
查看>>
PMCalendar
查看>>
【收藏】Aspose.Pdf应用教程
查看>>
PHP使用星号隐藏用户名,手机,邮箱的实现方法
查看>>
C++ 指针—01 指针与数组的对比
查看>>
推荐6款常用的Java开源报表制作工具
查看>>
CentOS mini安装环境下安装私有YUM服务器
查看>>
mysql case when 多参数条件语法
查看>>
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
查看>>
实现JSON在线美化(格式化)、JSON转CSV、CSV转XML工具-toolfk程序员工具网
查看>>
Combine Two Tables[leetcode]
查看>>
Linux环境变量
查看>>
Python2 进程扫描脚本
查看>>
JQuery EasyUI 日期控件如何控制日期选择区间
查看>>