[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生成指定个括号
		生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ... 
随机推荐
- html 关于内部是float元素的外部div高度为0的解决方法!
			最近编写一个页面的时候遇见一个问题,外部div是block的,而内部元素是float的,大家应该都知道float的元素是没有实际高度的,就算你设置了float元素的高度他也不会撑开外部block元素的 ... 
- 敏捷软件开发:原则、模式与实践——第14章 使用UML
			第14章 使用UML 在探索UML的细节之前,我们应该先讲讲何时以及为何使用它.UML的误用和滥用已经对软件项目造成了太多的危害. 14.1 为什么建模 建模就是为了弄清楚某些东西是否可行.当模型比要 ... 
- 敏捷开发中高质量 Java 代码开发实践
			Java 项目开发过程中,由于开发人员的经验.代码风格各不相同,以及缺乏统一的标准和管理流程,往往导致整个项目的代码质量较差,难于维护,需要较大的测试投入 和周期等问题. 这些问题在一个项目组初建.需 ... 
- WCF使用net.tcp寄宿到IIS中(转)
			一.IIS部分 环境:Windows Server 2008 R2 1.安装WAS,如下图所示: 2.网站net.tcp协议绑定,如下图所示: 3.网站启用net.tcp,如下图所示: 二 ... 
- 一个不错的shell 脚本教程 入门级
			一个很不错的bash脚本编写教程,至少没接触过BASH的也能看懂 建立一个脚本 Linux中有好多中不同的shell,但是通常我们使用bash (bourne again shell) 进行s ... 
- poj 2942 Knights of the Round Table  圆桌骑士(双连通分量模板题)
			Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ... 
- openfire+asmack搭建的安卓即时通讯(五) 15.4.12
			这一篇博客其实是要昨天写的,但昨天做了作修改就停不下来了,这次的修改应该是前期开发的最终回了,其余的功能有空再做了,下周可能要做一些好玩的东西,敬请期待! 1.修改下Logo:(Just We) ht ... 
- js立即执行函数: (function ( ){...})( ) 与 (function ( ){...}( )) 有区别?
			没有区别. 你需要明白 IIFE 的原理,我简单说一下: function foo() {...} // 这是定义,Declaration:定义只是让解释器知道其存在,但是不会运行. foo(); / ... 
- jquery模拟下拉框单选框复选Select,Checkbox,Radio
			在项目中,你会发现设计稿中常常会有单选框,复选框,但都不是系统默认的样式,这就可以用jquery来模拟它们:如图所示,实现它们所需要的代码如下: 首先需要引入的代码: <link rel=&qu ... 
- POJ 2653	 Pick-up sticks --队列,几何
			题意: 按顺序扔木棒,求出最上层的木棒是哪些. 解法: 由于最上层的木棒不超过1000个,所以用一个队列存储最上层的木棒,每次扔出一个木棒后,都与队列中的木棒一一判断,看此木棒是否在某一最上层的木棒的 ... 
