import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/generate-parentheses/
*
* Created by lverpeng on 2017/7/11.
*
* 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:
*
* "((()))", "(()())", "(())()", "()(())", "()()()"
*
*/
public class GenerateParentheses { /**
* 生成的括号个数是2n个(包括左括号和右括号),而且1-2n范围内左括号的个数一定大于右括号的个数
*
* @param n
* @return
*/
public List<String> generate (int n) {
List<String> list = new ArrayList<String>();
generate(n, n, list, "");
return list;
} /**
* 使用深度优先算法
*
* @param left 左括号的个数
* @param right 右括号的个数
* @param result 最终字符串的保存在这里
* @param str
*/
private void generate (int left, int right, List<String> result, String str) {
if (left == 0 && right == 0) {
result.add(str);
}
if (left > 0) {
generate(left - 1, right, result, str + "(");
} // 维护括号配对的规则,先有左括号,才能有右括号
if (left < right && right > 0) {
generate(left, right - 1, result, str + ")");
}
} public static void main(String[] args) {
GenerateParentheses generateParentheses = new GenerateParentheses();
List<String> list = generateParentheses.generate(2);
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
} }

leetcode — generate-parentheses的更多相关文章

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

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

  2. LeetCode: Generate Parentheses 解题报告

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

  3. [LeetCode]Generate Parentheses题解

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

  4. [LeetCode] Generate Parentheses 生成括号

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

  5. LeetCode Generate Parentheses (DFS)

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

  6. LeetCode——Generate Parentheses

    Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...

  7. LeetCode: Generate Parentheses [021]

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

  8. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  9. leetcode Generate Parentheses python

    # 解题思路:列举出所有合法的括号匹配,使用dfs.如果左括号的数量大于右括号的数量的话,就不能产生合法的括号匹配class Solution(object): def generateParenth ...

  10. 并没有看起来那么简单leetcode Generate Parentheses

    问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...

随机推荐

  1. redis 3.2.1 centos7

    设置外网访问 vmare:centos7redis:3.2.1 redis-cli shutdown 重启./redis-server 启动服务 启动之后无法访问 cd redis-3.2.1 vim ...

  2. java【基础】多态

    new 接口就会发生很有意思的现象 public class InerClassDemo { public static void main(String[] args) { // TODO Auto ...

  3. Alpha 冲刺 (6/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...

  4. 学习Acegi应用到实际项目中(4)

    此节介绍:ConcurrentSessionFilter. 在Acegi 1.x版本中,控制并发HttpSession和Remember-Me认证服务不能够同时启用,它们之间存在冲突问题. 在一些应用 ...

  5. maven学习三

    做项目时使用maven构建项目已经是现在的流行做法了.那么maven的作用是什么呢?maven中的几个常用的命令都有什么用?下面我们来看一下. maven最大的作用就是用于对项目中jar包依赖的统一管 ...

  6. shell脚本学习-printf命令

    跟着RUNOOB网站的教程学习的笔记 printf命令模仿C程序库里的printf()程序.printf由POSIX标准所定义,因此使用printf的脚本比使用echo有着更好的移植性. printf ...

  7. 【慕课网实战】Spark Streaming实时流处理项目实战笔记二十一之铭文升级版

    铭文一级: DataV功能说明1)点击量分省排名/运营商访问占比 Spark SQL项目实战课程: 通过IP就能解析到省份.城市.运营商 2)浏览器访问占比/操作系统占比 Hadoop项目:userA ...

  8. 我知道的nginx配置

    1.nginx配置文件 2.配置访问域名 #京淘商品管理系统 server { listen 80; server_name manage.jt.com; location / { proxy_pas ...

  9. java数组-如何在一堆数据中使用数组!

    数组 1.类型一致的一组数据,其实相当于集合概念. 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个数组元素(item),每个数组元素可以通过一个下标/索引来 ...

  10. CentOS No manual entry for man 没有 xx 的手册页条目

    yum install -y man man-pages man-pages-overrides https://unix.stackexchange.com/questions/182500/no- ...