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. MvcPager概述

    MvcPager 概述   MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottG ...

  2. ListView中RadioButton实现单项选择

    1:FragmentHack5.java public class FragmentHack5 extends Fragment { View view; ListView lvCountries; ...

  3. SSH自动部署

    我的是windows环境,目前开发的过程中,有些项目需要一下子部署到很多的linux服务器上.写了个脚本能够自动上传文件和执行部署任务.完成这个任务需要的条件包括SSH配置和一个执行脚本. 准备 1. ...

  4. 2015第15周六Java线程池

    Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 比较重要的几个类: Ex ...

  5. Linux权限机制

    权限是操作系统用来限制用户.组.进程对操作系统资源(文件.设备等)的访问的机制 权限分为:读.写.执行,一般表示为 r.w.x http://itercast.com/lecture/22 每个文件或 ...

  6. POJ1664(简单动态规划)

    #include<iostream> #include<string> #include<cstring> using namespace std; ][]; vo ...

  7. JAVA中常用需要设置的三个环境变量(JAVA_HOME、CLASSPATH、PATH)

    JAVA中常用需要设置的三个环境变量: JAVA_HOME.CLASSPATH.PATH (一) 配置环境变量:(相对路径) 1. JAVA_HOME=x:/jdk1.6.0 2. 用%JAVA_HO ...

  8. Android学习总结——判断网络状态

    package com.example.xch.broadcasttest; import android.content.BroadcastReceiver; import android.cont ...

  9. [Ext JS 4] 实战之Grid, Tree Gird编辑Cell

    前言 本篇这里以稍微复杂一点的Tree Grid 来介绍. 在写编辑grid 之, 先来看一下 grid 的 selType 的配置. 先给一个简单的Tree grid 的例子: Ext.onRead ...

  10. linux下git使用记录1 git 提交

    linux下git使用记录1   浏览:985 发布日期:2013/08/08 分类:技术分享 在使用github的时候,不可避免的接触到了git,用他来更新项目,做版本控制.这里特别把常用的命令记录 ...