LeetCode: Generate Parentheses 解题报告
Generate Parentheses
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
Hide Tags Backtracking String
SOLUTION 1:
我们还是使用九章算法的递归模板。
1. Left代表余下的'('的数目
2. right代表余下的')'的数目
3. 注意right余下的数目要大于left,否则就是非法的,比如,先放一个')'就是非法的。
4. 任何一个小于0,也是错的。
5. 递归的时候,我们只有2种选择,就是选择'('还是选择')'。
6. 递归的时候,一旦在结果的路径上尝试过'('还是选择')',都需要回溯,即是sb.deleteCharAt(sb.length() - 1);
public class Solution {
public List<String> generateParenthesis(int n) {
List<String> ret = new ArrayList<String>();
if (n == 0) {
return ret;
}
dfs(n, n, new StringBuilder(), ret);
return ret;
}
// left : the left Parentheses
// right : the right Parentheses
public void dfs(int left, int right, StringBuilder sb, List<String> ret) {
if (left == 0 && right == 0) {
ret.add(sb.toString());
return;
}
// left < right means that we have more ( then we can add ).
if (left < 0 || right < 0 || left > right) {
return;
}
dfs(left - 1, right, sb.append('('), ret);
sb.deleteCharAt(sb.length() - 1);
dfs(left, right - 1, sb.append(')'), ret);
sb.deleteCharAt(sb.length() - 1);
}
}
主页君的GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/GenerateParenthesis.java
LeetCode: Generate Parentheses 解题报告的更多相关文章
- 【LeetCode】Generate Parentheses 解题报告
[题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode]Generate Parentheses题解
Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
随机推荐
- 关于linux的添加永久静态路由的static-routes方法
一:使用 route 命令添加 使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法: //添加到主机的路由 # route add –host 192.168.1.11 dev ...
- 在Ubuntu下设置默认编辑器
在默认情况下,Ubuntu 系统会为用户预设程序.就拿文本编辑器来说吧,Ubuntu 预设的是 Nano,对某些朋友来说,使用 Vim 可能更得心应手些.那么如何更改这些预设的程序呢? 你可以使用 s ...
- windows installer服务无法启动,无法打开任何msi文件
如果不成功就在"依存关系"中找是否有其他的文件没有启用. 启用"remote procedure call(rpc)" 启用"workstation& ...
- 使用 C# 开发智能手机软件:推箱子(十二)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第十二篇.在这篇文章中,介绍 Window/AboutDlg.cs 源程序文件. 这个源程序文件包括 AboutDlg 类,该 ...
- 获取List对象的泛型类(原创)
群里一个伙计的需求,最后我提出了这种解决方案,不过他觉得多写俩括号增加了调用方的难度.还是先记下来吧,有时间看看还能不能再改造. 1.直接获取时获取不到的,类型被虚拟机擦除了2.利用子类实现父类的 ...
- iOS-APP启动页加载广告
概述 加载广告页, 展现跳过按钮实现倒计时功能, 并判断广告页面是否更新. 详细 代码下载:http://www.demodashi.com/demo/10698.html 目前市场上很多APP(如淘 ...
- Nginx中的安全配置
1.测试环境 操作系统:CentOS6.5 Web服务器:Nginx1.4.6 Php版本:Php5.4.26 2.Nginx介绍 1.nginx本身不能处理PHP,它只是个web服务器,当接收到请求 ...
- 给maven仓库添加镜像
用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...
- IOS 内存优化和调试技巧
基础部分 1: 图片内存大小小结 a: 图片:是占用内存的大户,尤其是手机游戏图片资源众多.对图片资源在内存中占用量的计算成为J2ME游戏开发者的经常性工作,CoCoMo来解释一下如何计算图片在内存中 ...
- PHP API中,MYSQL与MYSQLI的持久连接区别
转载自:http://www.cnxct.com/some-differences-between-mysql-and-mysqli-of-persistent-connection/ 很久很久以前, ...