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. SQLite的升级(转)

    做Android应用,不可避免的会与SQLite打交道.随着应用的不断升级,原有的数据库结构可能已经不再适应新的功能,这时候,就需要对SQLite数据库的结构进行升级了. SQLite提供了ALTER ...

  2. Android遍历获取指定目录的文件(转)

    // 获取当前目录下所有的mp4文件 public static Vector<String> GetVideoFileName(String fileAbsolutePath) { Ve ...

  3. plsql 常用快捷键(自动替换)

      plsql 常用快捷键 CreateTime--2018年4月23日17:33:05 Author:Marydon 说明:这里的快捷键,不同于以往的快捷键,输入指定字符,按快捷键,可以自动替换成你 ...

  4. 示例:Socket应用之简易聊天室

    在实际应用中,Server总是在指定的端口上监听是否有Client请求,一旦监听到Client请求,Server就会启动一个线程来响应该请求,而Server本身在启动完线程之后马上又进入监听状态. 示 ...

  5. vs.net使用技巧

    1.快捷键收缩和展开代码段 i. Ctrl-M-O   折叠所有方法 ii. Ctrl-M-P   展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M   折叠或展开当前方法 i ...

  6. js 随机变换图片

    <div style="position:absolute;left:40%;top:10%;border-style:dotted"> <img src=&qu ...

  7. Workflow_工作流的基本概念(概念)

    2014-06-01 Created By BaoXinjian

  8. DBA_实践指南系列10_Oracle Erp R12诊断功能Diagnostic(案例)

    2013-12-10 Created By BaoXinjian Thanks and Regards

  9. read/write函数与(非)阻塞I/O的概念

    一.read/write 函数 read函数从打开的设备或文件中读取数据. #include <unistd.h> ssize_t read(int fd, void *buf, size ...

  10. 最短路径 - 弗洛伊德(Floyd)算法

    为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是一个简单的3个顶点的连通网图. 我们先定义两个二维数组D[3][3]和P[3][3], D代表顶点与顶点 ...