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. Linux之find命令用于统计信息

    1. 计算当前目录中的文件数: [root@localhost tmp]# find . -type f | wc -l 2. 查找/etc目录中最新的和最旧的文件,以文件时间排序并按年-月-日的格式 ...

  2. BurpSuite导出log配合SQLMAP批量扫描注入点

    sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...

  3. ps命令使用 进程查看

    ps命令是Process Status的缩写 用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命 ...

  4. html php 重定向 跳转 刷新

    1秒后跳转 法一: <!DOCTYPE HTML> <html> <head>     <meta http-equiv="refresh" ...

  5. java 汽车销售收入系统

     >>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...

  6. hdu5092 dp(递推)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5092 题意:给一个m*n的矩阵,找到一个纵向的"线"使得线上的和最小并 ...

  7. 转:Delphi 回调函数及例子

    http://anony3721.blog.163.com/blog/static/5119742010866050589/ { http://anony3721.blog.163.com/blog/ ...

  8. Android HttpClient post MultipartEntity - Android 上传文件

    转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...

  9. js库写法

    前言: 现在javascript库特别多,其写法各式各样,总结几种我们经常见到的,作为自己知识的积累.而目前版本的 JavaScript 并未提供一种原生的.语言级别的模块化组织模式,而是将模块化的方 ...

  10. C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)

    Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, ...