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 ...
随机推荐
- HTTP in iOS你看我就够
HTTP属于老话题了,在项目中我们经常需要往服务端发POST或者GET请求,但是对于HTTP的了解不应只局限于此.千里之行,始于足下.越想走的远,基本原理就应该了解的透彻全面一些,仅仅停留在使用ASI ...
- git 取消/添加 某文件的跟踪
如果我们不小心将某个文件加入了 git 版本控制,但是突然又不想继续跟踪控制这个文件了,怎么办呢? 使用 git update-index 即可. 不想继续追踪某个文件 git update-inde ...
- Oracle 安装中遇到的问题
第一次用甲骨文,这期待!虽然mySQL也是甲骨文的. 去官网下了Oracle G11 R2 X64,本人的电脑是64位的win7,没开防火墙. 按照网上众多的教程,做完安装,可是安装过程不是那么的顺利 ...
- rest-简介
一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么,需要遵守什么样的准则. ...
- java计算两个日期之间相隔的天数
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- Web字体库下载及转换工具
1.字体现在网站: http://ztxz.org/ 2.Web字体在线格式转换器: http://www.freefontconverter.com/ 3.
- tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)
最近做一个demo,涉及按照数据分类然后依照分类在 cellForRowAtIndexPath形成不同类型的cell,就是有判断(在viewdidload里面做)和形成(在 cellForRowAtI ...
- javascript入门学习笔记
<button type="button" onclick="alert('Welcome!')">点击这里</button>alert ...
- 命令行bash的基础操作
刚进入系统在光标前面会显示这样一串字符[root@centeros ~]# root表示当前的登录用户可以通过id命令查看 centeros表当前的主机名可以通过hostname查看 ~表示当前用户的 ...
- javascript学习(知识点整理)
有了这个代码,就可以在定义 中增加更多的控制了 后面会举例关于extjs定义的更多控制 此种方案可以解决定义时需要一些函数调用的情况 函数作用域和声明提前: 即由于js是解释性语言,在执行前会 ...