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 ...
随机推荐
- sql遍历全部数据集
DECLARE @a int set @a = 1 while @a < 5 BEGIN select top(1) * from QPShuGameMatchDB..MatchScoreSta ...
- php-fpm 日志
1.php-fpm 错误日志 #默认位置 安装目录下的 log/php-fpm.log error_log = log/php-fpm.log #错误级别 alert(必须立即处理), error(错 ...
- (39)JS运动之缓冲运动
基本思路:使用定时器让物体向右运动,在运动的过程中再不是匀速运动,而是先快后慢,即距离越大,速度越快,距离越小,速度越小,可是到达终点的时候,必须注意要使用向上取整函数Math.ceil()和向下取整 ...
- 去掉cb中括号的匹配
Settings->Editor->General settings->Indent options->Brace completion``
- [转]网易云音乐Android版使用的开源组件
原文链接 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我 ...
- PostgreSQL与MySQL常用命令比较[转]
PostgreSQL与MySQL常用命令比较 原文链接: http://www.phpwell.com/?p=174 PostgreSQL MySQL 服务启动:1)#service postgres ...
- 安卓请求网络错误 直接在main Thread 进行网络操作出现maintreamexception
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites ...
- VSCode调试Html中的脚本 vscode前端常用插件推荐,搭建JQuery、Vue等开发环境 vsCode 添加浏览器调试和js调试的方法总结 VS Code - Debugger for Chrome调试js
一.背景 使用Visual Studio Code写了一个简单的Html页面,想调试下其中script标签里的javascript代码,网上查了一通,基本都是复制粘贴或者大同小异的文章,就是要安装De ...
- 帆软报表和jeecg的进一步整合--ajax给后台传递map类型的参数
下面是页面代码: <%@ page language="java" contentType="text/html; charset=UTF-8" page ...
- Android开发6——布局中的wrap_content和fill_parent以及match_parent
一.言简意赅 fill_parent 是让控件宽或者高占全屏 wrap_content是让控件的高或宽仅仅把控件里的内容包裹住而不是全屏 二.分别来看 1 fill_parent 设置一个构件的布局 ...