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:

"((()))", "(()())", "(())()", "()(())", "()()()"

题目大意:给一个数字n,返回所有的n对合法的小括号的组合。

解题思路一:回溯,DFS有回退的探索所有可能。先生成(1...N个)左括号,然后生成不多于左括号个数个右括号。

    public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
if (n == 0) {
return res;
}
btr("", res, 0, 0, n);
System.out.println(res);
return res;
} private void btr(String tmp, List<String> res, int left, int right, int n) { if (left == n && right == n) {
res.add(tmp);
return;
}
if (left < n)
btr(tmp + "(", res, left + 1, right, n);
if (right < left)
btr(tmp + ")", res, left, right + 1, n);
}

解题思路二:分治法。假设要求的是n,可以用F(n)表示n对括号所有的组合,那么有F(n)="("+F(i)+")"+F(n-i-1),i∈[0,n-1],划分为两个子问题,左半部分被一个括号包裹,右半部分没有被括号包裹,于是可以求得组合。参考这里。

public List<String> generateParenthesis(int n) {
LinkedList<String> res = new LinkedList<String>(); if (n == 0){
res.add("");
return res;
} else if (n == 1){
res.add("()");
return res;
} for(int i=n-1; i>=0; --i){
List<String> l = generateParenthesis(i);
List<String> r = generateParenthesis(n-i-1); for(String l_str : l){
for(String r_str : r){
res.add("(" + l_str + ")" + r_str);
}
}
} return res;
}

Generate Parentheses——LeetCode的更多相关文章

  1. Generate Parentheses - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Generate Parentheses - LeetCode 注意点 解法 解法一:递归.当left>right的时候返回(为了防止出现 )( ) ...

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

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

  3. Generate Parentheses leetcode java

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  4. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  5. Leetcode之回溯法专题-22. 括号生成(Generate Parentheses)

    Leetcode之回溯法专题-22. 括号生成(Generate Parentheses) 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n ...

  6. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

  7. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. 【LeetCode】22. Generate Parentheses (2 solutions)

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

  9. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

随机推荐

  1. javascript 鼠標拖動功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Java 理论与实践: 用弱引用堵住内存泄漏---转载

    要让垃圾收集(GC)回收程序不再使用的对象,对象的逻辑 生命周期(应用程序使用它的时间)和对该对象拥有的引用的实际 生命周期必须是相同的.在大多数时候,好的软件工程技术保证这是自动实现的,不用我们对对 ...

  3. 基于 Quartz 开发企业级任务调度应用--转

    Quartz 基本概念及原理 Quartz Scheduler 开源框架 Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现.该项目于 2009 ...

  4. POJ 1265 Area POJ 2954 Triangle Pick定理

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5227   Accepted: 2342 Description ...

  5. eclipse开发servlet应用,Tomcat无法访问jpg图片

    今天遇到个奇怪的问题,我发现我放在tomcat的webapps文件夹项目下的jpg文件,浏览器无法访问: 以前没有遇到过这个问题,我知道肯定能访问的,因为以前做过相关应用,不知道问题出在哪. 后来我把 ...

  6. JavaScript ArrayBuffer浅析

    时隔一年半,再次来到博客园.回首刚接触前端时所写的两篇随笔,无法直视啊~ --------------------------------------------------------------- ...

  7. struts2学生信息管理系统篇章③

    package com.java1234.util; public class PageUtil { //传进来四个参数,tagetUtil是跳转链接的头部,totalNum是总个数,currentP ...

  8. datazen 备份还原

    DataZen备份:   DataZen还原 第一步:用管理员进入dos,输入 net stop datazen 第二步:切换到datazen Core Service的安装目录cd c:\Progr ...

  9. HTML5 Canvas Text文本居中实例

    1.代码: <canvas width="700" height="300" id="canvasOne" class="c ...

  10. Android VideoView

    这两天公司要让做一个播放视频的小Demo,于是网上学习了下VideoView的使用方法. 先看布局文件,很简单 就是一个VideoView和两个ImageView <RelativeLayout ...