给一个整数n,找到所有合法的 () pairs 
 

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

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
递归程序:首先合法的pairs一定是左括号个数等于右括号个数。因此对于n对pairs,左括号个数为n个。
递归程序,按照括号个数进行判断。当左括号个数小于n时,str+”(“,当右括号个数小于左括号个数时,str+”)”
 
分析递归程序需要传递的参数:
首先要将保存结果的List<String> list传递进去,然后是每次修改之后的str,之后需要知道左括号个数 open ,还有右括号个数 close , 最后是最大括号个数为 max
 
private void backtrack(List<String> list,String str, int open, int close, int max)
 
当str长度等于2*max时,说明结束,此时需要执行 list.add(str)
当open < max 时 添加一个( 执行     backtrack ( list, str + ”(“, open+1, close, max);
当close < open 时 添加一个 ) 执行 backtrack ( list, str+ “)”, open, close +1 ,max);
 
因此执行顺序为: 先得到右括号个数连续个数为max时(从下标为0开始)的情况,在得到右括号连续个数为max-1 时的情况 (此时会进行执行添加右括号的命令) 
 
参考代码:
 
package leetcode_50;

import java.util.ArrayList;
import java.util.List; /***
*
* @author pengfei_zheng
* n对合理括号结果求解问题
*/
public class Solution22 {
public static List<String> generateParenthesis(int n) {
List<String> list = new ArrayList<>();
backtrack(list,"",0,0,n); return list;
}
private static void backtrack(List<String> list,String str, int open, int close, int max) {
if(str.length()==2*max){
list.add(str);
return;
}
if(open<max)
backtrack(list,str+"(",open+1,close,max);
if(close<open)
backtrack(list,str+")",open,close+1,max);
}
public static void main(String[]args){
List<String> list = generateParenthesis(3);
System.out.println(list);
}
}
 
 
 

LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)的更多相关文章

  1. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

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

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

  3. LeetCode 22. Generate Parentheses

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

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

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

  5. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

  6. Java [leetcode 22]Generate Parentheses

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

  7. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  8. [leetcode]22. Generate Parentheses生成括号

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

  9. [LeetCode] 22. Generate Parentheses ☆☆

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

随机推荐

  1. DATGRIDVIEW如何不选择第一行,第一列

    DataBindingComplete事件中写 ClearSelection();

  2. Intellij 2016非服务器激活

    下载 http://idea.lanyus.com/jar/JetbrainsCrack-2.6.2.jar 并将 JetbrainsCrack-2.6.0.jar 放置到 D盘根目录(也可以根据自己 ...

  3. 用外部物理路由器时与外部dhcp服务时怎样使用metadata服务(by quqi99)

    作者:张华  发表于:2015-12-31版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 用外部 ...

  4. HttpURLConnection和HttpClient的区别2(转)

    1.HttpClient比HttpURLConnection功能更强大,但是做java建议用前者,安卓建议用后者 2.这两者都支持HTTPS,streaming 上传与下载,配置超时时间,IPv6,  ...

  5. 组合模式(Composite Pattern) ------------结构型模式

    组合模式使用面向对象的思想来实现树形结构的处理和构件,描述了如何将容器对象和叶子对象进行递归组合,实现简单,灵活性好. 组合模式(Composite Pattern):组合多个对象形成树形结构以表示具 ...

  6. lrzsz离线安装方法

    lrzsz离线安装方法 到网上下载lrzsz安装包,这里以lrzsz-0.12.20.tar.gz为例 2 打开终端 cd 到安装包所在目录 tar zxvf lrzsz-0.12.20.tar.gz ...

  7. Win7 共享打印机 “错误:共享无法保存设置”

    原因:windows的后台防火墙服务进程[Windows Firewall{服务描述}.MpsSvc{服务名字}] 关闭了.services.smc 关闭 解决:重启服务 关闭防火墙的功能,通过控制面 ...

  8. 转:Hibernate query.list()之卡住问题

    某个函数里面有调用Query的list()方法,然后它有时会出现这种症状: 忽然停住不动,但是也没报异常,就是界面死了. 我的查询差不多是这样: Query q=sessionFactory.open ...

  9. ios的两种界面跳转方式

    1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...

  10. (翻译)2016美国数学建模MCM F题(政策)翻译:难民移民政策建模

    PROBLEM F:Modeling Refugee Immigration Policies With hundreds of thousands of refugees moving across ...