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 ...
随机推荐
- 多级联动下拉菜单--cxSelect
jquery cxSelect插件 github地址:https://github.com/ciaoca/cxSelect demo地址:http://code.ciaoca.com/jquery/c ...
- python mysql 查询返回字典结构
cur = self.conn.cursor(MySQLdb.cursors.DictCursor)加上MySQLdb.cursors.DictCursor可以返回字典结构 {列名:值} class ...
- 【转】 Pro Android学习笔记(六三):Preferences(7):代码控制首选项
[-] 代码实现preference 利用preference保存状态 DialogPreference 代码实现preference View可以不通过xml进行设置,有代码直接进行设置,首选项pr ...
- loadrunner的四个主要部件
1.VUser Generator :性能脚本开发 2.Controller :提供多线程并发操作 3.Analysis :结果分析 4.Load Generator :负载生成器 ***负载生成器完 ...
- web攻击之三:SQL注入攻击的种类和防范手段
观察近来的一些安全事件及其后果,安全专家们已经得到一个结论,这些威胁主要是通过SQL注入造成的.虽然前面有许多文章讨论了SQL注入,但今天所讨论的内容也许可帮助你检查自己的服务器,并采取相应防范措施. ...
- GCC提供的几个內建函数
参考 https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Other-Builtins.html#Other-Builtins https://en.wikipe ...
- 滑动swipe的妙用
转自:http://www.cnblogs.com/NEOCSL/archive/2013/03/04/2942861.html iterface ITouchable; function OnPic ...
- oracle--分页过程demo1
oracle分页过程demo1: --ROWNUM用法 select o.*,rownum rn from (select * from emp) o where rownum<=10; sel ...
- SpringMVC 学习笔记(请求方法的返回值和参数)
在用注解对配置 处理器时,一般是一个方法处理一个请求,不同方法的返回类型有着不同的意义. 返回值为 ModelAndView 类型 ModelAndView 是Model 和 View 的一个集合类型 ...
- 把Nutch爬虫部署到Hadoop集群上
原文地址:http://cn.soulmachine.me/blog/20140204/ 把Nutch爬虫部署到Hadoop集群上 Feb 4th, 2014 | Comments 软件版本:Nutc ...