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. Linux TCP队列相关参数的总结

    作者:阿里技术保障锋寒 原文:https://yq.aliyun.com/articles/4252 摘要: 本文尝试总结TCP队列缓冲相关的内核参数,从协议栈的角度梳理它们,希望可以更容易的理解和记 ...

  2. RabbitMQ和kafka从几个角度简单的对比--转

    业界对于消息的传递有多种方案和产品,本文就比较有代表性的两个MQ(rabbitMQ,kafka)进行阐述和做简单的对比, 在应用场景方面, RabbitMQ,遵循AMQP协议,由内在高并发的erlan ...

  3. Tomcat中Listener的使用范例(转载http://cywhoyi.iteye.com/blog/2075848)

    Tomcat是非常有名的开源容器,因其开源我们可以对其做定制化的改变,而且Tomcat在其配置文件方面做了很多注释说明摘要,帮助我们更好的定制化我们所需的功能点. New Tomcat Listene ...

  4. JDK5-静态导入

    import static 1. 导入一个类内所有静态成员 import static java.lang.Math.*; public class StaticImport { public sta ...

  5. LeanCloud使用入门(android)

    LeanCloud算是一个简单易用的云服务器,其中包含了强大的数据库支持,我们只需要将此服务器应用到本地的代码即可实现后台的存储与交互. 那么,如何简单实现本地代码和LeanCloud服务器的交互呢? ...

  6. Bellman_ford POJ 3259 Wormholes

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41728   Accepted: 15325 Descr ...

  7. C#—集合(Collection)

    1.栈(stack<T>) using System; using System.Collections.Generic; using System.Linq; using System. ...

  8. iOS中的设计模式

    一. MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离 ...

  9. 用css样式,为表格加入边框

    Table 表格在没有添加 css 样式之前,是没有边框的.这样不便于我们后期合并单元格知识点的讲解,所以在这一节中我们为表格添加一些样式,为它添加边框. 在右侧代码编辑器中添加如下代码: <s ...

  10. ASP.NET菜鸟之路之Response小例子

    背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Response.Write Redirect ...