https://leetcode.com/problems/generate-parentheses/

题目大意:给出n对小括号,求出括号匹配的情况,用列表存储并返回,例如:n=3时,答案应为:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
解题思路:想到递归的思想,判断匹配成功(递归返回)的条件为:左右括号数保持一致并且括号字符串的长度等于2*n。
具体做法:需要的空间:字符串列表存储最后结果;string存储括号;n存储要求的括号对数;deep存储字符串长度;left存储已经存入的左括号个数;right存储已经存入的右括号个数。
解题技巧:递归实现时一般给出的函数难以进行递归,需要一个帮助函数,我们重新定义一个函数来实现递归。
注意:s.resize(s.size() - 1);该句话是为了实现回溯,考虑一下每次执行该句时上一句的递归一定已经实现了,所以需要去除最后一个括号
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> result;
string s;
if(n > )
{
generateParenthesisHelper(result, s, n, , , );
}
return result;
}
void generateParenthesisHelper(vector<string>& result, string &s, int n, int deep, int left, int right)
{
if(deep == *n)
{
result.push_back(s);
return;
}
if(left < n)
{
s.push_back('(');
generateParenthesisHelper(result, s, n, deep + , left + , right);
s.resize(s.size() - );
}
if(right < left)
{
s.push_back(')');
generateParenthesisHelper(result, s, n, deep + , left, right +);
s.resize(s.size() - );
} } };

22. Generate Parentheses的更多相关文章

  1. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  2. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  3. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  4. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  5. 22. Generate Parentheses (recursion algorithm)

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

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

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

  7. LeetCode 22. Generate Parentheses

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

  8. 22. Generate Parentheses——本质:树,DFS求解可能的path

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

  9. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  10. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

随机推荐

  1. Servlet接收JSP参数乱码问题解决办法

    转自:http://lavasoft.blog.51cto.com/62575/274527/   环境: apache-tomcat-6.0.24.zip jdk1.6.0_16 WindosXP ...

  2. Python网络编程踩的坑

    错误:socket.error: [Errno 10013] 原因:端口号被占用 解决:换其他的端口号或者将其他应用的端口号关闭 错误:File "D:/pyworkspace/homewo ...

  3. 通过IIS发布站点和VS2012自带发布网站

    vs2012通过IIS发布站点 http://jingyan.baidu.com/article/0964eca2d7beeb8285f536bd.html 用VS2012自带发布网站 http:// ...

  4. PHP之CI框架架设错误--Only variable references should be returned by reference

    解决参考 http://www.rafalinux.com/ La búsqueda fue bastante infructuosa, y hasta hace un par de días, na ...

  5. BZOJ 1877 晨跑

    http://www.lydsy.com/JudgeOnline/problem.php?id=1877 思路:拆点费用流,答案就是最大流量和最小费用. #include<algorithm&g ...

  6. 暴力破解UltraEdit v21 无需注册

    一.复制一份UltraEdit安装目录中的主程序uedit32.exe,到任意目录,用UltraEdit打开复制的uedit32.exe文件. 二.修改以下内容 原来:00094750h: BE DC ...

  7. 把C#程序(含多个Dll)合并打包成单一文件

    实现的方式有多种. 1 Mono 项目中有一个工具,mono的一个附属工具mkbundle.(在Xamarin未被收购开源前,它是加密的商业软件.http://www.cnblogs.com/bins ...

  8. VS2010中使用QtOpenGL出现 unresolved external symbol __imp__glClear@4 referenced in function之类的错误

    描述: 链接了QtOpenGL4.lib QtOpend4.lib的库啊,居然还是发生此错误. 原因是没有链接OpenGL32.lib这个库.所以,要添加这个lib 重新rebuild的一下,此类的错 ...

  9. Spring MVC基础

    1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是一样的方法.框架只能在技术层 ...

  10. HHKB Professional 2

    今天买了一副号称程序员专用的静电容键盘 HHBK Pro2无刻版,简单上手了一下,确实名不虚传,打起字来酣畅淋漓,毫不拖泥带水,感觉自己的技术提高了不少呢!!!! 由于是无刻版,需要一些时间来适应,尤 ...