生成括号

给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。

样例

给定 n = 3, 可生成的组合如下:

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

解题

参考链接

采用递归树的思想

left: 左括号的数量

right:右括号数量

n:括号的对数

当left == n:表示左括号已经到达最大值了,只能添加右括号

当left >= right: 表示左括号数量 小于 右括号数量,可以添加左括号 也可以添加右括号

当left < right or left >n or right >n : 非法操作

  1. public class Solution {
  2. /**
  3. * @param n n pairs
  4. * @return All combinations of well-formed parentheses
  5. */
  6. public ArrayList<String> generateParenthesis(int n) {
  7. // Write your code here
  8. ArrayList<String> result = new ArrayList<String>();
  9. if(n<=0)
  10. return result;
  11. int left = 0,right=0;
  12. String str="";
  13. generateParenthesis(n,left,right,str,result);
  14. return result;
  15. }
  16. public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
  17. if(left >n || right >n || left < right)
  18. return;
  19. if(left == n){
  20. for(int i=0;i< n- right;i++)
  21. str+=")";
  22. result.add(str);
  23. return;
  24. }
  25.  
  26. // left>=right
  27. generateParenthesis(n, left+1, right,str+"(",result);
  28. generateParenthesis(n, left, right+1,str+")",result);
  29.  
  30. }
  31. }

下面的程序参考上面博客链接中,感觉判断添加是不是少了。

  1. public class Solution {
  2. /**
  3. * @param n n pairs
  4. * @return All combinations of well-formed parentheses
  5. */
  6. public ArrayList<String> generateParenthesis(int n) {
  7. // Write your code here
  8. ArrayList<String> result = new ArrayList<String>();
  9. if(n<=0)
  10. return result;
  11. int left = 0,right=0;
  12. String str="";
  13. generateParenthesis(n,left,right,str,result);
  14. return result;
  15. }
  16. public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
  17. if(left == n){
  18. for(int i=0;i< n- right;i++)
  19. str+=")";
  20. result.add(str);
  21. return;
  22. }
  23. generateParenthesis(n, left+1, right,str+"(",result);
  24. if(left>right){
  25. generateParenthesis(n, left, right+1,str+")",result);
  26. }
  27. }
  28. }

lintcode: 生成括号的更多相关文章

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. generate parentheses(生成括号)

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

  3. Leetcode 22.生成括号对数

    生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...

  4. [LintCode] Generate Parentheses 生成括号

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

  5. [LeetCode] Generate Parentheses 生成括号

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

  6. [leetcode]22. Generate Parentheses生成括号

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

  7. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

  8. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

  9. [LeetCode] 22. Generate Parentheses 生成括号

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

随机推荐

  1. (转)前端构建工具gulp入门教程

    前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...

  2. 条款24:若所有的函数参数可能都需要发生类型转换才能使用,请采用non-member函数

    假设有一个有理数类Rational,有一个计算有理数乘法的成员函数operator*,示例如下: #include <iostream> class Rational { public: ...

  3. JS中showModalDialog 详细使用

    基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...

  4. 如何把bootstrap用webpack打包

    今天下载了一个anguarl2写后台,一直没有找到是如何使用bootstrap样式的,然后就全文做了搜索,发现有一段代码 import 'bootstrap-loader'; 这段代码很可疑,所以就查 ...

  5. Postgresql 存储过程调试 1

    看来人真的有些力不从心,半个月前还很得意掌握的简单的Postgresql 存储过程的调试,一段时间没使用,做新功能就忘了! Postgresql 在开源的数据库里面算是很强悍的了,但现在就是不方便调试 ...

  6. Js作用域与作用域链详解[转]

     一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作 ...

  7. 20145120 《Java程序设计》第7周学习总结

    20145120 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda表达式 例:Comparator<String> byLength = (name1, na ...

  8. 关于四则运算的代码debug测试

    1.首先检测题目是否能为负数,0? 截图: 总结:如图所示出题数目为0的时候,并没提示重新输入,而是输出空白,而当输出题目为负数的时候系统提示错误,并且提示终止 2.检测操作值得范围:   总结:当操 ...

  9. 8、android代码优化技术记录

    1.length.length().size的优化 举例: int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5 ...

  10. discuz之同步登入

    前言   首先感谢dozer学长吧UCenter翻译成C# 博客地址----------->http://www.dozer.cc/   其次感谢群友快乐々止境同学的热心指导,虽然萍水相逢但让我 ...