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 ...
 
随机推荐
- isearch5 index,attribute和summary。
			
索引 isearch5 支持的索引分为:index,attribute和summary. Index指的是倒排索引,它存储了存储了从term到DocID的映射关系,形如: term-->(Doc ...
 - 函数式编程工具:filter和reduce
			
# -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具:filter和reduce #python内置函数中,map函数是用来进行函数式编程这类工具 ...
 - 6、javac命令详解
			
javac [ options ] [ sourcefiles ] [ @files ] 参数可按任意次序排列. options 命令行选项. sourcefiles 一个或多个要编译的源文件(例如 ...
 - SpringMVC 类内部的RequestMapping注解能否被继承?
			
首先注意标题,说的是类内部的注解 结论是: 不能,但是子类却可以享有父类中该注解带来的效果. 看了一下这个:http://elf8848.iteye.com/blog/1621392 自己也试了一下, ...
 - HighCharts画时间趋势图,标示区以及点击事件操作
			
最近在用HighCharts画趋势图,如果按照设计文档上来画那太复杂了,于是根据自己多年的经验改动了设计文档,添加了highcharts的标示区,然而我也发现,最后一次画highchart趋势图还是在 ...
 - springmvc ModelAndView 和 Model
			
@RequestMapping("") public ModelAndView index(HttpSession session) { Object data = session ...
 - static不实现多态
			
class Father { public static String getName() { return "father"; } } class Children extend ...
 - PAT 1087 All Roads Lead to Rome
			
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
 - android 搜索自动匹配关键字并且标红
			
这个效果主要是为了着重表现搜索关键字的 . 1. 单关键字匹配 若只需匹配 搜索 ...
 - <<Python基础教程>>学习笔记 | 第10章 | 充电时刻
			
第10章 | 充电时刻 本章主要介绍模块及其工作机制 ------ 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 一个简 ...