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

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

  1. [
  2. "((()))",
  3. "(()())",
  4. "(())()",
  5. "()(())",
  6. "()()()"
  7. ]
递归程序:首先合法的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 时的情况 (此时会进行执行添加右括号的命令) 
 
参考代码:
 
  1. package leetcode_50;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /***
  7. *
  8. * @author pengfei_zheng
  9. * n对合理括号结果求解问题
  10. */
  11. public class Solution22 {
  12. public static List<String> generateParenthesis(int n) {
  13. List<String> list = new ArrayList<>();
  14. backtrack(list,"",0,0,n);
  15.  
  16. return list;
  17. }
  18. private static void backtrack(List<String> list,String str, int open, int close, int max) {
  19. if(str.length()==2*max){
  20. list.add(str);
  21. return;
  22. }
  23. if(open<max)
  24. backtrack(list,str+"(",open+1,close,max);
  25. if(close<open)
  26. backtrack(list,str+")",open,close+1,max);
  27. }
  28. public static void main(String[]args){
  29. List<String> list = generateParenthesis(3);
  30. System.out.println(list);
  31. }
  32. }
 
 
 

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. linux中iptables的用法

    iptables基本操作笔记 一.基本操作 #启动防火墙 service iptables start #停止防火墙 service iptables stop #重启防火墙 service ipta ...

  2. [AngularJS]ng-repeat指令要点

    ng-repeat指令要点 1,基本格式,这里不作过多说明,需要的话查看文档 <div ng-repeat="item in someCollection [| someFilter: ...

  3. php 区分0和空

    能够区分出来的有2,4,6 方法 public function test(){ $test=; if($test==''){ echo '<br />在php中1,0即为空'; //被输 ...

  4. 5 云计算系列之glance镜像服务安装

    preface 在上节中我们了解了keystone服务,下面就看看glance管理镜像的服务吧. glance组成 glance有两部分组成: glance-api 接受云系统镜像的创建,删除,读取请 ...

  5. windows下安装node.js

    由于shopnc的im需要node.js 先安装下node.js 下载node.js 直接运行 安装完成后 win+R,出入cmd 安装时已经自动配置了环境变量(如果没设置环境变量,变量名:NODE_ ...

  6. java用substring函数截取string中一段字符串

    在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...

  7. selenium+phantomjs渲染网页

    from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapa ...

  8. okHttp3自用封装

    okHttp都已经出到3.2.0了,现在才开始要用到它,感觉自己好low~~ 根据平时自己的习惯,还是自己做一下封装,让代码撸起来更加顺畅一点! okhttp-3.2.0和okio-1.7.0就不多说 ...

  9. 禁用滚动视图ListView、ViewPager、ScrollView、HorizontalScrollView、WebView边界颜色渐变

    禁用滚动视图ListView.ViewPager.ScrollView.HorizontalScrollView.WebView边界颜色渐变 ListView.ViewPager.ScrollView ...

  10. Floyd算法解说

    開始知道Floyd算法是在<大话数据结构>这本书的无向带权图求最短路径看到的, 可是第一次没怎么看懂,所以就不看了,后来又看了两遍还是没明确,我以为是我理解能力有问题 后来从百度百科上看了 ...