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

Have you met this question in a real interview?

 
 
Example

Given n = 3, a solution set is:

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

LeetCode上的原题,请参见我之前的博客Generate Parentheses

解法一:

class Solution {
public:
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
if (n == ) return {};
vector<string> res;
helper(n, n, "", res);
return res;
}
void helper(int left, int right, string out, vector<string>& res) {
if (left < || right < || left > right) return;
if (left == && right == ) {
res.push_back(out);
return;
}
helper(left - , right, out + "(", res);
helper(left, right - , out + ")", res);
}
};

解法二:

class Solution {
public:
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
vector<string> generateParenthesis(int n) {
set<string> res;
if (n == ) {
res.insert("");
} else {
vector<string> pre = generateParenthesis(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 + , ')');
res.insert(a);
a.erase(a.begin() + i + , a.begin() + i + );
}
}
res.insert("()" + a);
}
}
return vector<string>(res.begin(), res.end());
}
};

[LintCode] Generate Parentheses 生成括号的更多相关文章

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. 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. 【使用Unity开发Windows Phone上的2D游戏】(1)千里之行始于足下

    写在前面的 其实这个名字起得不太欠当,Unity本身是很强大的工具,可以部署到很多个平台,而不仅仅是可以开发Windows Phone上的游戏. 只不过本人是Windows Phone 应用开发出身, ...

  2. 第一篇:SOUI是什么?

    概述 用C++做产品最痛苦的是什么?肯定是做UI. SOUI的使命就是把痛苦的UI变化成快乐的UI. 什么?UI还能快乐?脑子进水了吗? 当你看完这个系统教程的时候相信你面对UI至少不会再痛苦.你可以 ...

  3. UI中 frame 与 transform的用法与总结

    在iOS中,我们是不可以直接访问控件中frame的结构体的成员的,因此我们需要分三步来改变一个UI控件的位置,大小 一.frame用法 frame的结构体类型为: struct CGRect { CG ...

  4. windows内核需要注意的

    修改windows内核函数 先屏蔽KdPrint 测试. Hook函数一律使用全局变量 妹的..KiTrap0E 修改.触发了已经断点.但是硬件断点Hook函数里只要使用KdPrint 就蓝屏

  5. dreamwaver cs6 主题配色方案

    这是css代码效果 这是js效果 这是html效果 使用方法:1.将下列代码自制到一个文本文档中,将文档命名为Colors.xml. 2.将Colors.xml放到C:\Users\tom\AppDa ...

  6. vim使用02

    编辑 剪切光标所在的字符: <x>; 剪切并插入: <s> 撤销操作:撤销至上一个命令之间的修改: <u> 恢复上一次撤销操作: <C r> 剪切光标所 ...

  7. TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)

    TestNg使用dependsOnGroups属性来进行依赖测试, 测试方法依赖于某个或某些方法,这个/这些方法作为前置依赖条件 强制依赖:如果被依赖的某一个方法发生了异常,那么之后的方法都不会被执行 ...

  8. 01背包 ZOJ 3931 Exact Compression

    题目连接 题意:n个数字构建哈夫曼树,问是否存在这样一棵树使得:(Fi数字大小,Ci哈夫曼表示下,'0'的数量) 分析:每次从优先队列取出两个数字可以互换位置,这样可以01互换.设a[i] <= ...

  9. BFS HDOJ 2102 A计划

    题目传送门 题意:中文题面 分析:双层BFS,之前写过类似的题.总结坑点: 1.步数小于等于T都是YES 2. 传送门的另一侧还是传送门或者墙都会死 3. 走到传送门也需要一步 #include &l ...

  10. 转载:Java面试笔试题大汇总

    本文来源于:http://blog.csdn.net/wulianghuan 1.面向对象的特征有哪些方面 1).抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关 ...