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. iOS异步图片加载优化与常用开源库分析

    网络图片显示大体步骤: 1.下载图片: 2.图片处理(裁剪,边框等): 3.写入磁盘: 4.从磁盘读取数据到内核缓冲区: 5.从内核缓冲区复制到用户空间(内存级别拷贝): 6.解压缩为位图(耗cpu较 ...

  2. Java基础知识强化之集合框架笔记10:Collection集合使用的步骤

    集合使用的步骤: (1)创建集合对象 (2)创建元素对象 (3)把元素添加到集合 (4)遍历集合:       • 通过集合对象获取迭代器对象 • 通过迭代器对象的hasnext()方法判断是否有元素 ...

  3. java编程思想-注解思维导图

  4. git针对Android Studio的使用

    1.将文件放到项目根目录下 .gitignore 文件内容: *.iml.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_ ...

  5. linux中挂载硬盘报错(you must specify the filesystem type)

    公司有台服务器做了raid1,由于容量小,需扩容,原先打算再添加两块硬盘进去做多一组raid1,组成两组raid1混合使用,但是公司抠门,买到服务器只能安装3块硬盘,无奈之下只能放多一块进去单独挂载分 ...

  6. 为什么struts2 ajax 方法执行两次

    struts2中使用json插件执行ajax处理时,如果方法名是get方法的时候,方法会莫名其妙的执行两次. 原因: struts2 中JSON的原理是在ACTION中的get方法都会序列化,所以前面 ...

  7. js 求两个日期之间相差天数

    //求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne. ...

  8. windows批处理命令之ren

    1.批处理批量修改文件后缀名(假设我需要把一个文件夹中的很多txt文件改为sql文件): 1)在需要被处理的文件的文件夹里先新建一个txt文本,然后在文本中写入: ren *.txt *.sql 2) ...

  9. ul ol 列表的样式的控制

    ul( Unordered List)无序列表 ol(Ordered List)有序列表 列表的样式: 列表原有符号.自定义图形符号.符号显示位置. 1.列表符号 是显示于每一个列表项目前的符号标识. ...

  10. Kohana框架ORM类的基本使用

    1.首先需要创建一个模型类,以user为例,在application/classes/model/user.php路径下创建user.php,并且一个表对应一个模型,且表名必须在类名后加“S”,即表名 ...