Generate Parentheses——LeetCode
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
题目大意:给一个数字n,返回所有的n对合法的小括号的组合。
解题思路一:回溯,DFS有回退的探索所有可能。先生成(1...N个)左括号,然后生成不多于左括号个数个右括号。
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
if (n == 0) {
return res;
}
btr("", res, 0, 0, n);
System.out.println(res);
return res;
}
private void btr(String tmp, List<String> res, int left, int right, int n) {
if (left == n && right == n) {
res.add(tmp);
return;
}
if (left < n)
btr(tmp + "(", res, left + 1, right, n);
if (right < left)
btr(tmp + ")", res, left, right + 1, n);
}
解题思路二:分治法。假设要求的是n,可以用F(n)表示n对括号所有的组合,那么有F(n)="("+F(i)+")"+F(n-i-1),i∈[0,n-1],划分为两个子问题,左半部分被一个括号包裹,右半部分没有被括号包裹,于是可以求得组合。参考这里。
public List<String> generateParenthesis(int n) {
LinkedList<String> res = new LinkedList<String>();
if (n == 0){
res.add("");
return res;
} else if (n == 1){
res.add("()");
return res;
}
for(int i=n-1; i>=0; --i){
List<String> l = generateParenthesis(i);
List<String> r = generateParenthesis(n-i-1);
for(String l_str : l){
for(String r_str : r){
res.add("(" + l_str + ")" + r_str);
}
}
}
return res;
}
Generate Parentheses——LeetCode的更多相关文章
- Generate Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Generate Parentheses - LeetCode 注意点 解法 解法一:递归.当left>right的时候返回(为了防止出现 )( ) ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- Generate Parentheses leetcode java
题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)
Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
- 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- LeetCode: Generate Parentheses 解题报告
Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...
随机推荐
- linux c 系统报错
本文中的错误是指在代码编译完全正确程序可运行的情况下,因为没有成功调用程序中的某些系统调用函数而产生的错误.往往这些系统调用函数通过返回值(比如1,0,-1)来说明其是否调用成功,而程序员需要知道详细 ...
- Android上使用OpenGLES2.0显示YUV数据
在Android上用OpenGLES来显示YUV图像,之所以这样做,是因为: 1.Android本身也不能直接显示YUV图像,YUV转成RGB还是必要的: 2.YUV手动转RGB会占用大量的CPU资源 ...
- 刚入门的easyui
这两天看了下easyui的教学先说说自己的一些小小理解吧! ----在使用easyui中也遇到了一个问题 : Uncaught TypeError:cannot call method ‘offset ...
- VB几种函数参数传递方法,Variant,数组,Optional,ParamArray
VB几种函数参数传递方法,Variant,数组,Optional,ParamArray 一) 过程的参数被缺省为具有 Variant 数据类型. 1)ByRef按 地址传递参数在 VB 中是缺省的 按 ...
- HDU5289
题意:求解存在最大差值小于给定K值的所有区间段. 输入: T(测试数据) n(数组个数)K(给定区间值的范围) ai...(数组值) 输出: ss(所有满足符合条件的区间段) 思路:二分+ST算法,首 ...
- document.all用法
document.all用法 一. document.all是页面内所有元素的一个集合.例如: document.all(0)表示页面内第一个元素二.document.all可以判断浏览器 ...
- Wpf控件ListBox使用实例2
2.Xaml绑定选择结果 <StackPanel Orientation="Vertical"> <TextBlock Margin="10,10,10 ...
- WBS说明
work breakdown structure(WBS) 工作分解结构 (英语:Work Breakdown Structure, WBS)是一个详尽的,层次的(从全面到细节)的树形结构,由可交付成 ...
- 【USACO 3.3.2】商品购物
[描述] 在商店中,每一种商品都有一个价格(用整数表示).例如,一朵花的价格是 2 zorkmids (z),而一个花瓶的价格是 5z .为了吸引更多的顾客,商店举行了促销活动. 促销活动把一个或多个 ...
- 交叉编译:cannot find /lib/libc.so.6 collect2: ld returned 1 exit status
1.有时候明明指定了交叉编译的动态库搜索路径,但有些库提示还是搜索不到,而且提示的搜索路径有点奇怪,不是指定的路径,比如: /opt/mips-4.4/bin/../lib/gcc/mips-linu ...