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. javascript 调试技巧

    不用alert,用console.log() <!DOCTYPE html> <html> <head> <script type="text/ja ...

  2. quote、cite、refer的区别

    quote指“直引”,直接引用原文,不做丝毫修改. cite指“间引”,引用时不需要相同的词. refer指消化原来的思想,完全不抄.

  3. JS 中html 动态替换

    一.定义通用替换js函数,或调用JQuery验证的$.format函数: //----通用JS操作// var a = "我喜欢吃{0},也喜欢吃{1},但是最喜欢的还是{0},偶尔再买点{ ...

  4. jQuery 常用的代码片段

    1.  官网下载链接: http://jquery.com/download/ 2.  引入jquery 文件方式: (1) <script src="http://libs.baid ...

  5. 用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  6. [嵌入式学习资料]ARM开发学习详解iTOP-4412开发板使用手册

    拿到的最新4412开发板学习使用手册,完全免费,分享一下 下载地址:http://pan.baidu.com/s/1ntrJA8h

  7. CORDIC原理与FPGA实现(1)

    CORDIC算法的来历与用途大家网上随处可以见到,这里写 一下自己的理解. 将P(x,y)旋转角度a得到新的坐标P’(x’,y’).这里的坐标变换为: x’= x cos(a) – y sin(a)  ...

  8. notepad++下的字体设置

    设置 - 语言格式设置 中

  9. ZOJ 3829 Known Notation --贪心+找规律

    题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...

  10. 2014 Super Training #10 D 花生的序列 --DP

    原题: FZU 2170 http://acm.fzu.edu.cn/problem.php?pid=2170 这题确实是当时没读懂题目,连样例都没想通,所以没做了,所以还是感觉这样散漫的做不好,有些 ...