Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给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,生成括号对,递归,深度优先搜索。的更多相关文章
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- python 递归深度优先搜索与广度优先搜索算法模拟实现
一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
随机推荐
- [LintCode] A + B 问题
Bit-by-Bit summation: class Solution { public: /* * @param a: The first integer * @param b: The seco ...
- 170223、Tomcat部署时war和war exploded区别以及平时踩得坑
war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: 是选择war还是war exploded 这里首先看一下他们两个的区别: war模式 ...
- Dart基础学习03--方法的使用
1.本文主要讲一下Dart中的方法是怎么定义的,下面先看一个简单的例子: void printNumber(num number) { print('The number is $number.'); ...
- python拓展库whl下载网址集合:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
- 使用Dell R710 IDRAC挂载虚拟介质
Dell DRAC,虚拟介质分离或所选虚拟磁盘驱动器的虚拟介质重定向已由另一用户使用 DELL Idrac 一台Dell的R710服务器,远程管理器后发现虚拟介质无法映射,一直提示“虚拟介质分离或所选 ...
- linux运维 vi vim q 的间接注释
w q --不发生写的写,无增删效果. 点q后,再次执行 vi /var/www/share/w.php 仍然会‘ Found a swap file by the name "/var/ ...
- centos7在vmware上无法上网
centos7在虚拟机中设置NAT后也无法上网! 首先激活网卡!打开桌面右键在终端中打开:cd /etc/sysconfig/network-scripts/ls 找到以ifcfg开头的,如ifcfg ...
- 转!!Java的三种代理模式
转自 http://www.cnblogs.com/cenyu/p/6289209.html 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象 ...
- django--个人主页建立练习
1.前端页面采用模板继承与动态模板 {% extends 'base.html' %} {% block content %} {% for article in article_list %} &l ...
- git发布代码到github过程和常见错误
在对git有了基本了解之后,并且常常看到很多人在github上发布代码和开源项目时,就会想如何也把自己的代码发布到github上,并能够不断的跟踪版本变化. 现在就有几个想要做的事. 一.如何把自己已 ...