题目

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

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

"((()))", "(()())", "(())()", "()(())", "()()()"

题解

这道题跟unique binary tree ii是类似的。如果是只求个数的话是类似unique binary tree,用到了卡特兰数。

这里也是用到了类似的模型。

不过这道题按照DFS那种递归想法解决还是比较容易想到的。

给定的n为括号对,所以就是有n个左括号和n个右括号的组合。

按顺序尝试知道左右括号都尝试完了就可以算作一个解。

注意,左括号的数不能大于右括号,要不然那就意味着先尝试了右括号而没有左括号,类似“)(” 这种解是不合法的。

代码如下:

 1     public ArrayList<String> generateParenthesis(int n) {  

 2         ArrayList<String> res = new ArrayList<String>();

 3         String item = new String();

 4         

 5         if (n<=0)

 6             return res;  

 7             

 8         dfs(res,item,n,n);  

 9         return res;  

     }  

       

     public void dfs(ArrayList<String> res, String item, int left, int right){ 

         if(left > right)//deal wiith ")("

             return;

             

         if (left == 0 && right == 0){  

             res.add(new String(item));  

             return;  

         }

         

         if (left>0) 

             dfs(res,item+'(',left-1,right);  

         if (right>0) 

             dfs(res,item+')',left,right-1);  

     } 

Reference:

http://blog.csdn.net/linhuanmars/article/details/19873463

http://blog.csdn.net/u011095253/article/details/9158429

Generate Parentheses leetcode java的更多相关文章

  1. Generate Parentheses - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Generate Parentheses - LeetCode 注意点 解法 解法一:递归.当left>right的时候返回(为了防止出现 )( ) ...

  2. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  3. Generate Parentheses——LeetCode

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

  4. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  5. Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  6. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  7. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  8. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  9. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

随机推荐

  1. Xamarin iOS教程之视图显示图像

    Xamarin iOS教程之视图显示图像 Xamarin iOS显示图像 在主视图中显示一个图像,可以让开发者的应用程序变的更有趣,例如,在一些应用程序开始运行时,都会通过图像来显示此应用程序的玩法或 ...

  2. Xamarin iOS教程之进度条和滚动视图

    Xamarin iOS教程之进度条和滚动视图 Xamarin iOS 进度条 进度条可以看到每一项任务现在的状态.例如在下载的应用程序中有进度条,用户可以很方便的看到当前程序下载了多少,还剩下多少.Q ...

  3. 3143 二叉树的序遍历codevs

    题目描述 Description 求一棵二叉树的前序遍历,中序遍历和后序遍历 输入描述 Input Description 第一行一个整数n,表示这棵树的节点个数. 接下来n行每行2个整数L和R.第i ...

  4. [Java]进程与线程的区别(转)

    线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)地址空间:进程内的一个执行单元;进程至少有一个线程;它们共享进程的地址空间;而进程有自己独立的地址空间; (2)资源拥有: ...

  5. BZOJ 3245: 最快路线 spfa

    3245: 最快路线 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3245 Description 精明的小R每每开车出行总是喜欢走最快 ...

  6. CentOS 7下软阵列的创建

    硬阵列和软阵列没有什么可比性,毕竟应用的场景不一样,比如硬阵列性能强劲,有额外的内存支持缓存使其更快,但也有缺点,以硬盘为个数,且如果其中某个硬盘的容量不齐,那么会以最小的容量为准,体现出硬阵列的不灵 ...

  7. How to detect the types of executable files

    How to detect the types of executable files type { IMAGE_DOS_HEADER: DOS .EXE header. } IMAGE_DOS_HE ...

  8. Both - Either - Neither English Grammar

    http://www.grammar.cl/english/both-either-neither.htm Both, Either, Neither Summary Chart We use bot ...

  9. setInterval设置停止和循环

    原文链接:http://caibaojian.com/setinterval-times.html 需要知道已经经过了多少次或者说过多久就会停止 var timesRun = 0; var inter ...

  10. 打印函数 lodop

    Lodop属性和方法详解 例子:LODOP.PRINT_INIT("打印任务名");LODOP.SET_PRINT_COPIES(2);bdhtml=window.document ...