给一个整数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. js 正则去重

    split():字符串中的方法,把字符串转成数组. sort():数组中的排序方法,按照ACALL码进行排序. join():数组中的方法,把数组转换为字符串 var demo="ababb ...

  2. linux中kill命令

    Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后台进程就须 ...

  3. mysql用户管理 常用sql语句 mysql数据库备份恢复

     

  4. 自定义控件?试试300行代码实现QQ侧滑菜单

    Android自定义控件并没有什么捷径可走,需要不断得模仿练习才能出师.这其中进行模仿练习的demo的选择是至关重要的,最优选择莫过于官方的控件了,但是官方控件动辄就是几千行代码往往可能容易让人望而却 ...

  5. 通过tarball形式安装HBASE Cluster(CDH5.0.2)——配置分布式集群中的YARN ResourceManager 的HA

    <?xml version="1.0"?> <!-- Licensed under the Apache License, Version 2.0 (the &q ...

  6. 【python】并行化的又一种思路

    https://segmentfault.com/a/1190000000414339

  7. linux下主流的三种远程技术

    远程登录操作对于租用服务器的用户来说都不陌生.特别是租用国外服务器的用户来说,更是家常便饭.通过远程登录操作,即使我们人在深圳,也能无差别的操作远在美国的服务器.而对于linux系统下的服务器,目前主 ...

  8. 常见bootloader介绍

    https://blog.csdn.net/weibo1230123/article/details/82716818 http://fasight001.spaces.eepw.com.cn/art ...

  9. linux proc目录介绍

    https://www.cnblogs.com/DswCnblog/p/5780389.html

  10. Ajax请求全局配置

    摘要: jQuery已经成为项目中最常见的js库,也是前端开发最喜欢使用的库.下面是在项目中封装了jQuery的Ajax,分享给大家. 代码: // ajax 请求参数 var ajaxSetting ...