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. 6J - 盐水的故事

    挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下:然后滴二滴,停一下:再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则 ...

  2. MPLAB X IDE V4.15 创建工程,编译,问题处理

    初步接触,有错误的地方还请大神们务必提出来,防止误导他人 硬件环境:MCU--PIC18F67K22 仿真下载器--ICD 3 编译环境:MPLAB X IDE V4.15 中文版 工作需要接触到了P ...

  3. centos7 go ENV 部署

    1.wget官网下载go 官网https://golang.org/dl/ 2.解压 tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz 3.配置环境 ...

  4. 9. Bookshops in London 伦敦书店

    9. Bookshops in London 伦敦书店 (1) Londoner are greater readers.They buy vast numbers of newspapers and ...

  5. bzoj4556(sam)

    二分答案,(具体可见http://blog.csdn.net/neither_nor/article/details/51669114),然后就是判定问题,sa和sam都可以做,用sam写了一下,先用 ...

  6. modelsin联合仿真

    1-选择eda仿真工具  tool->options->eda tool options 2-assignments->settings->eda tool settings- ...

  7. Vuejs——(10)组件——父子组件通信

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...

  8. RabbitMQ基本理论

    本节内容 一  RabbitMQ介绍 二  RabbitMQ安装配置 三  RabbitMQ的Python实现-pika 1. 生产者消费者 2. 工作队列 3. 持久化和公平分发 4. 发布与订阅 ...

  9. 记录使用 Cake 进行构建并制作 nuget 包

    书接上一回(https://www.cnblogs.com/h82258652/p/4898983.html)?[手动狗头] 前段时间折腾了一下,总算是把我自己的图片缓存控件(https://gith ...

  10. kaldi的TIMIT实例二

    ============================================================================ MonoPhone Training & ...