[CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses.
EXAMPLE
Input: 3
Output: ((())), (()()), (())(), ()(()), ()()()
LeetCode上的原题,请参见我之前的博客Generate Parentheses 生成括号。
解法一:
class Solution {
public:
vector<string> generateParens(int n) {
set<string> t;
if (n == ) t.insert("");
else {
vector<string> pre = generateParens(n - );
for (auto a : pre) {
for (int i = ; i < a.size(); ++i) {
if (a[i] == '(') {
a.insert(a.begin() + i + , '(');
a.insert(a.begin() + i + , ')');
t.insert(a);
a.erase(a.begin() + i + , a.begin() + i + );
}
}
t.insert("()" + a);
}
}
return vector<string>(t.begin(), t.end());
}
};
解法二:
class Solution {
public:
vector<string> generateParens(int n) {
vector<string> res;
generateParensDFS(n, n, "", res);
return res;
}
void generateParensDFS(int left, int right, string out, vector<string> &res) {
if (left > right) return;
if (left == && right == ) res.push_back(out);
else {
if (left > ) generateParensDFS(left - , right, out + '(', res);
if (right > ) generateParensDFS(left, right - , out + ')', res);
}
}
};
[CareerCup] 9.6 Generate Parentheses 生成括号的更多相关文章
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
随机推荐
- window平台下的MySQL快速安装。(不好意思,未完成待续,请飘过)
MySQL安装方式 MSI安装(Windows Installer) ZIP安装 最好选择ZIP安装,比较干净,也快速搞好. 下载链接:http://pan.baidu.com/s/1sjFZZul ...
- 内部类--毕向东Java基础教程学习笔记
内部类的访问规则 1. 内部类可以直接访问外部类的成员,包括私有. 之所以可以直接访问外部类的成员,是因为内部类中持有外部类的引用,格式:外部类名.this 2.外部类要访问内部类,必须建立内部类对象 ...
- linux把EDT时间修改为CST格式
初始时间:2012年 09月 14日 星期五 18:15:33 EDT [root@test ~]# mv /etc/localtime /etc/localtime.bak [root@test ~ ...
- MySql分类
基础 数据类型宽度 查看字段长度 数据类型 运算符 函数 查询 插入 更新 删除 索引 自定义存储过程和函数 视图 触发器 权限管理 备份和恢复 日志 优化 复制
- 由IP和掩码计算广播地址
public static IPAddress GetBroadcast(IPAddress ipAddress, IPAddress subnetMask) { var ip = ipAddress ...
- JavaScript中点号“.”的多义性
点号「.」在JavaScript中有两种语义 语义1.表示算术中的小数点(浮点数),如 2.5 语义2.取对象属性.方法,如 [].push(2) 这几乎没有任何难理解的地方,但下面这个问题则很有趣. ...
- SQL SERVER 2012 使用订阅发布同步数据库
软件做大了,客户就多了,一个数据库服务器是远远不够的,当有一台数据服务器卦掉,那整个系统就会崩溃,所以必须考虑到数据库的自动同步与备份,当一台数据库服务 器宕机,自然就有用一台数据服务器启动起来保证整 ...
- 【转载】chromium浏览器开发系列第一篇:如何获取最新chromium源码
背景: 最近摊上一个事儿,领导非要让写一篇技术文章,思来想去,自己接触chrome浏览器时间也不短了,干脆就总结一下吧.于是乎,本文顺理成章.由于有些细节必需描述清楚,所以这次先讲如何拿到ch ...
- Kafka原理与java simple producer示例
brokers和消费者使用zk来获取状态信息和追踪消息坐标. 每一个partition是一个有序的,不可变的消息序列. 只有当partition里面的file置换到磁盘文件以后,才开放给消费者来消费. ...
- pycharm 4.5在debian下安装
1.去官网下载linux下的Tar包,下载后解压. 2.直接进入解压后的folder里面找bin下面的pycharm.sh,执行后发现没有任何反应. 3.查询资料发现是因为pycharm需要sun j ...