给 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. vmware station中 UDEV 无法获取共享存储磁盘的UUID,症状: scsi_id -g -u -d /dev/sdb 无返回结果。

    1.确认在所有RAC节点上已经安装了必要的UDEV包 [root@11gnode1 ~]# rpm -qa|grep udevsystem-config-printer-udev-1.1.16-25. ...

  2. UOJ #348 州区划分 —— 状压DP+子集卷积

    题目:http://uoj.ac/problem/348 一开始可以 3^n 子集DP,枚举一种状态的最后一个集合是什么来转移: 设 \( f[s] \) 表示 \( s \) 集合内的点都划分好了, ...

  3. ElasticSearch logo 分布式搜索引擎 ElasticSearch

    原文来自:http://www.oschina.net/p/elasticsearch Elastic Search 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中 ...

  4. 【转】Pro Android学习笔记(二六):用户界面和控制(14):RelativeLayout

    相对布局:RelativeLayout RelativeLayout也是非常常用的布局,能够精确对控件的位置进行网格对齐,可以设置在控件与其他控件的相对位置,以及控件在容器中的位置.缺省控件的位置为最 ...

  5. 请问两个div之间的上下距离怎么设置

    转自:https://zhidao.baidu.com/question/344630087.html 楼上说的是一种方法,yanzilisan183 <div style="marg ...

  6. MS-SQL使用xp_cmdshell命令导出数据到excel

    exec master..xp_cmdshell 'bcp "select c.Category_Title as 标题,p.Category_Title as 所属分类 from ltbl ...

  7. linux下将编译错误输出到一个文本文件

    linux下将编译错误输出到一个文本文件 command > filename 把把标准输出重定向到一个新文件中 command > > filename 把把标准输出重定向到一个文 ...

  8. javascript的DI

    学习AngularJS的原因之一就是觉得他的DI很牛叉,为了更好的学习,在研究源码之前,想自己按照自己的思路先实现个DI,希望这个思路能够对学习源码有帮助. (function(){ var conf ...

  9. cygwin运行git submodule init出错error while loading shared libraries的解决

    installing the Devel\gettext package should solve your problem. git-submodule requires that. Unfortu ...

  10. windows 下隐藏 system 函数弹窗

    概述 下面的程序是解决windows 下面调用 system() 函数的时候,会有窗口弹出的问题 头文件 #include <windows.h> 源码 /** * @brief 普通字符 ...