给一个整数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. 使用w查看系统负载 vmstat命令 top命令 sar命令 nload命令

    w/uptime 查看系统负载 w查看系统负载,uptime跟w一样. [root@centos7 ~]# w 22:34:10 up 6 days, 23:10,  4 users,  load a ...

  2. IOS_多线程

    苹果的Cocoa框架支持的多线程机制有三中NSThread.GCD.NSOperation. NSThread:是官方推荐的也是最主要的线程创建方式,可是须要开发这自己去管理线程的生命周期比如线程同步 ...

  3. sql产生随机数

    使用RAND(),结果是类似于这样的随机小数:0.615942003695649 SELECT FLOOR(RAND()*N) ---生成的数是这样的:12.0  SELECT CAST(FLOOR( ...

  4. Adb 获取手机信息

    adb shell getprop [ro.product.board]: [herring][ro.product.brand]: [google][ro.product.cpu.abi2]: [a ...

  5. 怎么用ABBYY在线浏览PDF文件

    ABBYY FineReader 让您可以从在线存储服务中打开图像或 PDF 文件,并将已识别文本保存至在线存储服务中,如 Dropbox.SkyDrive 或 Google Drive 等.通过在 ...

  6. Python爬虫-爬取科比职业生涯高清图集

    前面学习了Python爬取豆瓣电影Top250的数据,爬取的信息是电影信息的文本信息,但是在互联网上流行的图片才有更大的吸引力,本篇我们来使用python爬取网页上的图片并保存在本地硬盘上,很兴奋吧, ...

  7. windows 环境内网超快同步 DFS

    记录下: 在WINDOWS环境下,内网同步使用DFS可以超快实现文件同步,效果非常OK 纯粹记录下!

  8. mysql中json_object函数的使用?

    需求说明: 今天看了json_object函数的使用,在此记录下使用过程 操作过程: 1.使用json_object函数将一个键值对列表转换成json对象 mysql> select json_ ...

  9. docker machine介绍和使用

    https://www.cnblogs.com/sparkdev/p/7044950.html https://www.jianshu.com/p/cc3bb8797d3b

  10. linux 按照端口一句命令杀死进程,按照进程名称一句命令杀死进程

    例如杀死nginx 按照程序名称杀死进程 例如杀死nginx的进程 ps -aux|grep nginx|grep -v grep|cut -c 9-15|xargs kill -9 或者 ps -a ...