给 n 对括号,写一个函数生成所有合适的括号组合。
比如,给定 n = 3,一个结果为:
[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
详见:https://leetcode.com/problems/generate-parentheses/description/

由于字符串只有左括号和右括号两种字符,而且最终结果必定是左括号3个,右括号3个。定义两个变量left和right分别表示剩余左右括号的个数,如果在某次递归时,左括号的个数大于右括号的个数,说明此时生成的字符串中右括号的个数大于左括号的个数,即会出现')('这样的非法串,所以这种情况直接返回,不继续处理。如果left和right都为0,则说明此时生成的字符串已有3个左括号和3个右括号,且字符串合法,则存入结果中后返回。如果以上两种情况都不满足,若此时left大于0,则调用递归函数,注意参数的更新,若right大于0,则调用递归函数,同样要更新参数。

实现语言:Java

class Solution {
public List<String> generateParenthesis(int n) {
List<String> res=new ArrayList<String>();
generateParenthesisDFS(n,n,"",res);
return res;
}
void generateParenthesisDFS(int left,int right,String out,List<String> res){
if(left>right){
return;
}
if(left==0&&right==0){
res.add(out);
}else{
if(left>0){
generateParenthesisDFS(left-1,right,out+"(",res);
}
if(right>0){
generateParenthesisDFS(left,right-1,out+")",res);
}
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4444160.html

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

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

  8. LeetCode 022 Generate Parentheses

    题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...

  9. LeetCode OJ:Generate Parentheses(括号生成)

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

随机推荐

  1. Java中读取输入方式的性能比较

    程序开发过程中,需要从键盘获取输入值是常有的事,但Java它偏偏就没有像c语言给我们提供的scanf(),C++给我们提供的cin()获取键盘输入值的现成函数!Java没有提供这样的函数也不代表遇到这 ...

  2. GPIO编程1:用文件IO的方式操作GPIO

    概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,GPIO 的 ...

  3. js中this

    首先声明,我是小白,以下只是自己的简单理解. 先看下面的代码: (function () { console.log(this); })(); 毫无疑虑,输出的是window. 在看下面代码: (fu ...

  4. c++17 filesystem, regex 遍历目录

    linux 遍历目录+文件(优化版本) c++17 FS 还是挺好用的, VS2017支持,但是linux g++7.3 还是不支持 filesystem #include<filesystem ...

  5. ss3

    1)执行如下命令,进行安装shadowsocks(命令需要一条条的输入,然后回车,执行完一条,在执行下一条命令),过程中可能会让你选择确认的地方,直接输入"y"回车即可: yum ...

  6. day18-事务与连接池 4.事务特性

  7. Spring开发环境搭建

    ----------------siwuxie095 Spring 是运行在 Java 环境下的开发框架,因此在开发前需要 准备以下相关软件: JDK 7 或者以上版本 Eclipse 4 或以上版本 ...

  8. java单链表反转

    今天做leetcode,遇到了单链表反转.研究了半天还搞的不是太懂,先做个笔记吧 参考:http://blog.csdn.net/guyuealian/article/details/51119499 ...

  9. 在虚拟机环境(CentOS7系统)下将kubernetes中部署服务成功,但在虚拟机外部无法访问到服务

    在CentOS7环境下,kubernetes单机版环境,成功部署一个服务,在虚拟机中访问服务没问题,下面这样: curl http://172.27.73.26:8888/eureka-server/ ...

  10. asp.net Page.Controls对象(找到所有服务器控件)

    前台 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...