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 生成括号的更多相关文章

  1. generate parentheses(生成括号)

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

  2. [LintCode] Generate Parentheses 生成括号

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

  3. [LeetCode] Generate Parentheses 生成括号

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

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

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

  5. [leetcode]22. Generate Parentheses生成括号

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

  6. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

  7. 22.Generate Parentheses[M]括号生成

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

  8. LeetCode OJ:Generate Parentheses(括号生成)

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

  9. 22. Generate Parentheses生成指定个括号

    生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...

随机推荐

  1. ASP.NET @Page指令属性(vs2010)

    最近看一篇好文章,摘抄下来. 原文出处:http://www.cnblogs.com/zhaozhan/archive/2010/05/01/1725819.html @Page指令位于每个ASP.N ...

  2. Java部分总结图片版(已经加上原图链接下载!!!)

    Java基础知识图片版(原图下载链接)

  3. 每日Scrum--No.9

    Yesterday:测试软件 Today:写阶段性的总结 Problem: (1)晚上我们的团队进行了收尾工作:第一阶段的任务基本完成,软件主要实现了校园景点照片以及对应的介绍,查询最短路径,查询涉及 ...

  4. JavaScript Patterns 2.11 Writing Comments

    Document all functions, their arguments and return values, and also any interesting or unusual algor ...

  5. 利用mysql对特殊字符和超长字符会进行截断的特性 进行存储型XSS攻击——WordPress <4.1.2 & <=4.2 存储型xss

    转自:Baidu Security LabXteam http://xteam.baidu.com/?p=177 漏洞概述 本次漏洞出现两个使用不同方式截断来实现的存储型xss,一种为特殊字符截断,一 ...

  6. HttpClent4.3 的例子

    package com.unbank.robotspider.util; import java.io.IOException; import java.net.MalformedURLExcepti ...

  7. 精----Java读取xml文件的四种方法

    xml文件: Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VA ...

  8. 用shell脚本批量修改文件后缀名

    早上本想将一些照片上传到相册中,但是由于所有照片的扩展名都是JPG而不是小写的jpg,因此造成了“格式不正确”而不能上传照片.此刻就产生了这样一个问题:使用shell脚本如何批量将所有文件的扩展名JP ...

  9. beeline vs hive cli

    近期,大数据开发环境升级为cloudera 5.3. 配套的hive版本升级为0.13.1.可以使用心仪已久的分析开窗函数了.但在使用的过程中发现一些问题,仅记于此. 1.在使用hive命令的时候,发 ...

  10. [转]设定version 更新js缓存

    http://zhenggm.iteye.com/blog/680600 遇到的问题:         在访问量比较大的系统中,我们需要将一些静态的文件在客户端缓存,以减少下载的流量,从而加快客户端访 ...