022 Generate Parentheses 生成括号
给 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 生成括号的更多相关文章
- [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 ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode 022 Generate Parentheses
题目描述:Generate Parentheses Given n pairs of parentheses, write a function to generate all combination ...
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- BZOJ1217:[HNOI2003]消防局的设立
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- 洛谷 1351 联合权值——树形dp
题目:https://www.luogu.org/problemnew/show/P1351 对拍了一下,才发现自己漏掉了那种拐弯的情况. #include<iostream> #incl ...
- IOS的设计模式
对象创建 原型(Prototype) 使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象. NSArray *array = [[NSArray alloc] initWithObject ...
- linux 下 安装mysql
安装之前,因为redhat 是yum自带的,但是不能使用,因为要交钱,还要订阅,所以需要卸载,重新安装163提供的yum 在另外一篇文章介绍 yum list mysql* 列出所有关于mysal的安 ...
- #410(div2)B. Mike and strings
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- [51nod1138]正整数分解为几个连续自然数之和
解题关键:注意为什么上界是$\sqrt {2n} $ 因为函数是关于m的递减函数,而结果必须为正整数 $a = \frac{{2n + m - {m^2}}}{{2m}} = \frac{n}{m} ...
- 主线程与UI线程简介
---------------siwuxie095 Java 程序的主线程 当 Java 程序启动时,一个线程立刻运行,该线程通常叫做程 ...
- Angular06 组件、模块、父子组件之间的数据传递
1 创建组件 进入到angular项目的根目录,执行如下命令 ng g component test-component 注意:执行完上述命令后在angular项目的src/app文件夹下就会多出一个 ...
- [原创]SQL表值函数:返回自定义时间段的日期数据
跟以往类似,我依旧介绍一个我日常开发遇到的知识点,谨此记录一下,也希望能帮助到一些朋友. 这次我要介绍的是通过SQL函数返回你输入的两个时间点内的日期数据. 效果图如下: 执行函数:SELECT * ...
- Centos下添加/删除用户
useradd具体参数 [root@yhwang ~] useradd -h Usage: useradd [options] LOGIN useradd -D useradd -D [options ...