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 解题报告的更多相关文章

  1. 【LeetCode】Generate Parentheses 解题报告

    [题目] Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  2. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  5. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  6. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  7. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  8. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  9. 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

随机推荐

  1. 【Android开发经验】Cannot generate texture from bitmap异常的解决方式

    异常现象: 今天在处理用户头像的过程中,由于头像的处理比較复杂,由于,没有使用afinal自带的自己主动载入.而是自己依据头像的下载路径.手动进行下载和使用.可是在手动回收bitmap对象的过程中,会 ...

  2. oracle 批量更新之将一个表的数据批量更新至另一个表

      oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...

  3. iOS-高仿支付宝手势解锁(九宫格)

    概述 高仿支付宝手势解锁, 通过手势枚举去实现手势密码相对应操作. 详细 代码下载:http://www.demodashi.com/demo/10706.html 基上篇[TouchID 指纹解锁] ...

  4. HTML:调用静态页面html 的几种方法

    今天做办公用品管理系统时,发现需要用到在一个静态页面html 中调用多个静态页面html的内容.查找资料总结了以下一些方法: 一.iframe引入的方法 代码如下: <!-- 部门--> ...

  5. Oracle游标解析

    本节对Oracle中的游标进行详细讲解. 本节所举实例来源Oracle中scott用户下的emp表dept表: 一.游标: 1.概念: 游标的本质是一个结果集resultset,主要用来临时存储从数据 ...

  6. CLH队列锁

    http://blog.csdn.net/aesop_wubo/article/details/7533186 CLH锁即Craig, Landin, and Hagersten (CLH) lock ...

  7. django 创建数据库表 命令

    一旦你觉得你的模型可能有问题,运行 python manage.py validate . 它可以帮助你捕获一些常见的模型定义错误. 模型确认没问题了,运行下面的命令来生成 CREATE TABLE ...

  8. nginx配置文件中location说明

    1 nginx配置文件 文件结构 ... #全局块 events { #events块 ...} http #http块 { ... #http全局块 server #server块 { ... #s ...

  9. Xilinx全局时钟

    前言 Xilinx系列.ISE环境中,设计复杂工程时全局时钟系统的设计显得尤为重要. 一.时钟网络与全局缓冲 在XilinxFPGA中,时钟网络分为两类:全局时钟网络和I/O区域时钟网络.以全铜工艺实 ...

  10. 3dmax坐标系与导出fbx的坐标系

    3dmax和opengl都是右手坐标系,但是3dmax是z轴向上,而opengl中是Y轴向上.如图: 所以在3dmax的fbx导出对话框中有“轴转化”一项,可以设置“Y向上”或者“Z向上”. 默认是“ ...