问题描述:给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. JPA的坑多服务主键重复

    配置如下 @Id @GeneratedValue(generator="generator") @GenericGenerator(name="generator&quo ...

  2. ES6入门概览一

    一.let const 1.let命令:声明变量仅在块级作用域有效:let实际上为js新增了块级作用域 好处: -不存在变量提升: -内部声明的变量 函数 不会影响外部 -不可重复声明变量 2.con ...

  3. MySQL 索引性能分析概要

    上一篇文章 MySQL 索引设计概要 介绍了影响索引设计的几大因素,包括过滤因子.索引片的宽窄与大小以及匹配列和过滤列.在文章的后半部分介绍了 数据库索引设计与优化 一书中,理想的三星索引的设计流程和 ...

  4. 巨蟒python全栈开发数据库攻略1:基础攻略

    1.什么是数据库? 2.数据库分类 3.数据库简单介绍 4.安装数据库 5.修改root密码 6.修改字符集 7.sql介绍 8.简单sql操作

  5. window.onbeforeunload 埋点 页面停留时间

    window.onbeforeunload - Web API 接口 | MDN https://developer.mozilla.org/zh-CN/docs/Web/API/Window/onb ...

  6. 转!!SQL左右连接中的on and和on where的区别

    原博文地址:http://blog.csdn.net/xingzhemoluo/article/details/39677891 原先一直对SQL左右连接中的on and和on where的区别不是太 ...

  7. IOS研究之IOS7四种后台机制

     IOS 7中,实际上APP拥有四种后台模式.不管是哪一种后台机制,均须要利用苹果给予的对应后台接口实现.IOS7系统中,开发人员能够灵活利用多种后台接口(API)实现更加智能的应用操作. 对获取 ...

  8. leetcode -day 15 Distinct Subsequences

    1.  Distinct Subsequences  Given a string S and a string T, count the number of distinct subsequen ...

  9. phpexcel导出带生成图片完美案例

    // 导出exl public function look_down(){ $id = I('get.id'); $m = M ('offer_goods'); $where['offer_id'] ...

  10. C#框架及概念

    EF框架