1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. /**
  5. * Source : https://oj.leetcode.com/problems/generate-parentheses/
  6. *
  7. * Created by lverpeng on 2017/7/11.
  8. *
  9. * Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
  10. *
  11. * For example, given n = 3, a solution set is:
  12. *
  13. * "((()))", "(()())", "(())()", "()(())", "()()()"
  14. *
  15. */
  16. public class GenerateParentheses {
  17. /**
  18. * 生成的括号个数是2n个(包括左括号和右括号),而且1-2n范围内左括号的个数一定大于右括号的个数
  19. *
  20. * @param n
  21. * @return
  22. */
  23. public List<String> generate (int n) {
  24. List<String> list = new ArrayList<String>();
  25. generate(n, n, list, "");
  26. return list;
  27. }
  28. /**
  29. * 使用深度优先算法
  30. *
  31. * @param left 左括号的个数
  32. * @param right 右括号的个数
  33. * @param result 最终字符串的保存在这里
  34. * @param str
  35. */
  36. private void generate (int left, int right, List<String> result, String str) {
  37. if (left == 0 && right == 0) {
  38. result.add(str);
  39. }
  40. if (left > 0) {
  41. generate(left - 1, right, result, str + "(");
  42. }
  43. // 维护括号配对的规则,先有左括号,才能有右括号
  44. if (left < right && right > 0) {
  45. generate(left, right - 1, result, str + ")");
  46. }
  47. }
  48. public static void main(String[] args) {
  49. GenerateParentheses generateParentheses = new GenerateParentheses();
  50. List<String> list = generateParentheses.generate(2);
  51. System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
  52. }
  53. }

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. android studio 关闭SVN关联

    <?xml version="1.0" encoding="UTF-8"?> <project version="4"&g ...

  2. SQLite3命令操作大全

    SQLite3命令操作大全 SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.ql ...

  3. 深入C#的String类

  4. Spring mvc解决url传递中文参数乱码问题

    在tomcat服务器中,修改server.xml参数,如<Connector URIEncoding="UTF-8" connectionTimeout="2000 ...

  5. [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creator【s

    问题:执行创建函数的sql文件报错如下: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ...

  6. 《MarkMark学习笔记学习笔记》html学习笔记

    iframe里有一个srcdoc属性,很有用! window.location.href=document.referrer//可以实现返回上一级页面并刷新 HTML5权威指南©®,比较老的书了,有些 ...

  7. C++基础笔记(string截取)

    #include <iostream> #include <string> using namespace std; int main(int argc, char* argv ...

  8. Android studio 中的TabWidget

    1.TabWidget 的 layout文件 <?xml version="1.0" encoding="utf-8"?> <TabHost ...

  9. Java 实现将其他类型数据转换成 JSON 字符串工具类

    这是网上一个大神实现的,具体出处已找不到,在这做个记录,方便以后使用. package com.wb.test; import java.beans.IntrospectionException; i ...

  10. 本地调用QQ只需要一句代码

    如下图:点击在线客服以后,弹出QQ登录框 经测试,如果已经登录QQ,可能会提示版本不支持该功能,让你升级,但并不一定就是说你QQ版本需要更新,只是因为你QQ已经登录 有的浏览器可能因为出于安全考虑,会 ...