Level:

  Medium

题目描述:

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

For example, given n = 3, a solution set is:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

思路分析:

  用left和right代表左括号和右括号的剩余数,初始值为n,利用回溯的思想解题,当出现left的值大于right的值时,说明串中的右括号多于左括号,()),这种直接错误返回,如果出现left和right都为零则是满足情况的一个串。

代码:

public class Solution{
public List<String>generateParenthesis(int n){
List<String>res=new ArrayList<>();
if(n<=0)
return res;
int left=n;
int right=n;
String str="";
help(left,right,res,str);
return res;
}
public void help(int left,int right,List<String>res,String str){
if(left<0||right<0||right<left)
return;
if(left==0&&right==0){
res.add(str);
return;
}
help(left-1,right,res,str+"(");
help(left,right-1,res,str+")");
}
}

26.Generate Parentheses(生产有效括号的种类)的更多相关文章

  1. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

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

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

  3. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  4. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  5. [LintCode] Generate Parentheses 生成括号

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

  6. Generate Parentheses

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

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

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

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

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

  9. [LeetCode]Generate Parentheses题解

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

随机推荐

  1. sonarLint 插件配置sonarQube Server

    Connected Mode You can bind Eclipse projects to a SonarQube project (supporting SonarQube servers 5. ...

  2. How to fix apt-get GPG error NO_PUBKEY Ubuntu 14

      This morning when I do apt-get update on my new Ubuntu 14.04 server, I got these error messages: R ...

  3. SQL和NoSQL

    SQL和NoSQL 目前的数据库系统非常多,有传统的关系型的数据库系统(又被称为SQL数据库系统),有最近几年流行起来的NoSQL数据库系统.其中NoSQL数据库系统又分为很多种不同的类型,根据各个系 ...

  4. Python的Flask框架使用Redis做数据缓存的配置方法

    flask配置redis 首先得下载flask的缓存插件Flask-Cache,使用pip下载. sudo pip install flask_cache 为应用扩展flask_cache   app ...

  5. A Look at the Razor View Engine in ASP.NET MVC

    The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the ne ...

  6. DataSet、DataTable转换List(泛型集合与DataSet互相转换 )

    using System.Data; using System.Reflection; using System.Collections; using System.Collections.Gener ...

  7. 算法描述》LCA两三事(蒟蒻向)

    LCA是图论中常用的解决树形结构子问题的工具,这一问题一般需要用一个简短的子函数直接解决,但是这对于广大蒟蒻们仍然是一个不小的问题. LCA是指在树形结构中两点的最近公共祖先,对于这个问题,直接向上找 ...

  8. dubbo参数调优

    dubbo中配置优先级规律:方法级配置优先级高于接口级,consumer的优先级高于provider. 详细: consumer的method配置  >  provider的method配置 c ...

  9. 10个强大的Javascript表单验证插件推荐

    创建一个JavaScript表单验证插件,可以说是一个繁琐的过程,涉及到初期设计.开发与测试等等环节.实际上一个优秀的程序员不仅是技术高手,也应该是善假于外物的.本文介绍了10个不错的JavaScri ...

  10. zlib编程

    一.简介 zlib是提供数据压缩用的函式库,使用DEFLATE算法,最初是为libpng函式库所写的,后来普遍为许多软件所使用,今天,zlib是一种事实上的业界标准.   二.基本信息 数据头(hea ...