给一个整数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. mysql的字符串函数

    From: http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/12/13/1904330.html 对于针对字符串位置的操作,第一个位置被标 ...

  2. Mac或者linux下登陆到linux上的SFTP

    登陆 sftp  -i  密钥路径  用户@ip ➜  ~ sftp -i Desktop/aliyun.pem root@39.106.30.1 Connected to 39.106.30.1 上 ...

  3. 编写高质量代码_改善C++程序的150个建议 读书笔记

    这几天看了下这本书<编写高质量代码_改善C++程序的150个建议>,觉的蛮有收获的,再次记录下自己以前不清晰的知识点,以供学习. 编写符合标准的main函数 C语言标准规定了main函数的 ...

  4. GridView通过RowDataBound事件获取字段值、数据源列值

    如果数据源有某一列,而在绑定到GridView时不显示该列,编程时要用到该列的值可以用以下方法: protected void GridView1_RowDataBound(object sender ...

  5. VS2015常用快捷键

    1.回到上一个光标位置/前进到下一个光标位置  1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2.复制/剪切/删除整行代 ...

  6. session没保存,登录失败

    今天发现做的项目,登录不上 查了下原因,原来是session没保存上 查看php.ini文件,session.save_path="D:\php\tmp\tmp" 查看了下对应的目 ...

  7. iOS app开发入门

    https://github.com/qinjx/30min_guides/blob/master/ios.md

  8. JSP输出当前日期

    如何在网页中动态交互,输出当前日期? <%@ page language="java" import="java.util.*" contentType= ...

  9. win7 、2008 提示Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/ 的错误解决方法

    在安装控件过程中出现提示Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/ 的错误解决方法 1. 点 ...

  10. 将文件导入到SQL server数据库表中的字段中

    一.在要执行的sql server数据库a中执行如下脚本,创建存储过程sp_textcopy /* 将二进制文件导入.导出到数据库相应字段列中 */ CREATE PROCEDURE sp_textc ...