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. JavaWeb学习笔记--Servlet代码集

    目录: 登录系统提交表单数据打开PDFCookieURL传递参数URL重写跟踪会话使用HttpSession对象跟踪会话Servlet间协作过滤器Filter 登录系统 <!DOCTYPE HT ...

  2. Qt修改文件内容

    在用Qt进行嵌入式开发的时候,有时需要通过界面永久的改变ip地址等网卡信息.此时只能修改系统中包含网卡信息的文件,下图红框中所示就是文件中的网卡信息. 那么如何修改这四行呢,我的做法是先打开该文本文件 ...

  3. 【转】Android源码学习(2)使用Git和Repo进行版本管理

    原文网址:http://blog.chinaunix.net/uid-26074270-id-2458828.html Android项目采用Git和Repo进行版本管理.在大多数情况下,Git都可以 ...

  4. Android_Studio常用插件

    Android studio常用插件,可极大简化开发,增强开发效率. 不懂安装studio插件,看参考博文:android stuido插件安装:http://blog.csdn.net/liang5 ...

  5. Populating Next Right Pointers in Each Node II 解答

    Question Follow up for problem "Populating Next Right Pointers in Each Node". What if the ...

  6. bzoj4546-codechef XRQRS(可持久化Trie)

    中文题题意我就不说了 解析: 可持久化Trie的模板题,详见注释 代码 #include<cstdio> #include<cstring> #include<strin ...

  7. 【POJ1581】A Contesting Decision(简单模拟)

    没有什么弯路,直接模拟即可.水题. #include <iostream> #include <cstring> #include <cstdlib> #inclu ...

  8. Python Cookie HTTP获取cookie并处理

    Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...

  9. VPN拨号后使用本地网络上网

    网络环境大概是这样了:我在家里用ADSL上网,通过VPN连接到公司的服务器.但是连接VPN后,只能登录到公司的服务器,与INTERNET就断开了,QQ.网页都断开了.公司的服务器应该是连网的,可能被限 ...

  10. xcode6和ios 8 百度无法定位解决

    . @interface里: CLLocationManager *locationManager; . 初始化: locationManager = [[CLLocationManager allo ...