问题描述:给n对括号,生成所有合理的括号对。比如n=2,(()),()()

算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索。边界条件是n==0;前面电话号组成字符串也是利用dfs。

  1. public List<String> generateParenthesis(int n) {
  2. List<String> result = new ArrayList<>();
  3. dfs(result,"",n,n);
  4. return result;
  5. }
  6. public void dfs(List<String> result, String s, int left, int right)//n个左括号,n个右括号
  7. {
  8. if(left > right)//右括号比左括号多,无法生成。直接返回。截枝。
  9. {
  10. return;
  11. }
  12. if(left == 0 && right == 0)//递归的边界条件。
  13. {
  14. result.add(s);
  15. }
  16. if(left > 0)
  17. {
  18. dfs(result, s+"(", left-1, right);
  19. }
  20. if(right > 0)
  21. {
  22. dfs(result, s+")", left, right-1);
  23. }
  24. }

Generate parentheses,生成括号对,递归,深度优先搜索。的更多相关文章

  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. [LintCode] Generate Parentheses 生成括号

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

  4. [LeetCode] Generate Parentheses 生成括号

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

  5. [LeetCode] 22. 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. 022 Generate Parentheses 生成括号

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

  8. python 递归深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...

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

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

  10. 22. Generate Parentheses生成指定个括号

    生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...

随机推荐

  1. matplotlib绘制圆饼图

    import matplotlib.pyplot as plt labels = ['Nokia','Samsung','Apple','Lumia'] values = [10,30,45,15] ...

  2. 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262

    POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...

  3. JD-GUI

    JD-GUI http://jd.benow.ca/ JD-GUI可到官網直接下載.官網除了JD-GUI之外,另提供了Eclipse(JD-Eclipse)和IntelliJ(JD-IntelliJ) ...

  4. Properties 集合

    Map Hashtable Properties 特点: 该集合中的键和值都是字符串类型 集合中的数据可以保存到流中, 或者从流中获取 应用: 通常该集合用于操作以键值对形式存在的配置文件 常用方法: ...

  5. js基础练习--控制多组图片切换

    js基础练习题,一个按钮控制两组图片的切换,做这题的时候我忽然想到了将num1.mun2……都存放在一个数组中,根据索引值匹配到对应相应组的图片,这样不管有多少组图片都简单的搞定切换,可惜js基础都没 ...

  6. 《深入理解Linux网络技术内幕》阅读笔记 --- 路由表

    路由表基本概念 1.路由是由多个不同的数据结构的组合来描述的,每个数据结构代表路由信息的不同部分.例如,一个fib_node对应一个单独的子网,一个fib_alias对应一条路由.这样做的原因是只需通 ...

  7. (数据库之pymysql)

    权限管理http://www.cnblogs.com/linhaifeng/articles/7267587.html#_label6一.pymysql模块(安装与查询) 1.安装pymysql(py ...

  8. Notepad++ 更换主题

    到Textmate Theme Directory下载主题: 用文本编辑器把它打开,复制所有代码,贴到theme converter page里,然后“Download”: 保存: 在Notepad+ ...

  9. lamp中的Oracle数据库链接

    lamp一键安装包: https://lnmp.org/install.html 在CentOS 6.7 64位安装PHP的PDO_OCI扩展 Installing PDO_OCI extension ...

  10. Python(递归)

    递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以 ...