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

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

public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
dfs(result,"",n,n);
return result;
}
public void dfs(List<String> result, String s, int left, int right)//n个左括号,n个右括号
{
if(left > right)//右括号比左括号多,无法生成。直接返回。截枝。
{
return;
}
if(left == 0 && right == 0)//递归的边界条件。
{
result.add(s);
}
if(left > 0)
{
dfs(result, s+"(", left-1, right);
}
if(right > 0)
{
dfs(result, s+")", left, right-1);
}
}

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. 如何修改Myeclipse的JSP模板

    先找到MyEclipse的安装目录, 再找到myeclipse/eclipse/plugins/com.genuitec.eclipse.wizards_5.1.0/templates/jsp (co ...

  2. 第19章—后端分页(PageHelper)

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  3. Java方法区和永久代

    Java方法区和永久代 目前有三大Java虚拟机:HotSpot,oracle JRockit,IBM J9. JRockit是oracle发明的,用于其WebLogic服务器,IBM JVM是IBM ...

  4. Android---55---Web Service概述

    Web Service 是什么? /*w3school*/ Web Services 是应用程序组件 Web Services 使用开放协议进行通信 Web Services 是独立的(self-co ...

  5. Java-idea-生成JavaDoc

    1.选中要生成的doc模块 2.选择Tools→Generate JavaDoc 3.填写一下信息 1. 选择是整个项目还是模块还是单个文件 2. 文档输出路径 4. 传入JavaDoc的参数,一般这 ...

  6. node.js---sails项目开发(1)

    1.安装Node.js和npm---这里就做介绍啦! 2.需要全局下安装Sails sudo npm install sails -g 3. 在本地创建一个文件夹 mkdir ~/lsg/sails ...

  7. TypeScript学习笔记—数据类型

    TypeScript 数据类型 Boolean 类型 let isDone: boolean = false; // tsc => var isDone = false; Number 类型 l ...

  8. Django-form进阶+详细版

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 一.创建Form类 #!/usr/bin/en ...

  9. Django中间件(含Django运行周期流程图)

    Django中的中间件(含Django完整生命周期图) Django中间件简介 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,djang ...

  10. pycharm断点调试

    step over 执行下一步 蓝色高亮的那一行表示准备执行的代码