生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边)

思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号

有点像二叉树的建立过程

/*
思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号
判断好两种添加的条件后向后添加就行:
1.当左边括号不超过括号数n时可以添加左括号
2.当右括号不超过左括号时可以添加右括号
用递归依次向下添加就行
由于这种数据结构比较像二叉树,代码使用二叉树写的,其实完全不需要用二叉树。
*/
//这里不能写public,要不LeetCode不给通过
class TreeNode {
public StringBuilder val;
public TreeNode left;
public TreeNode right;
public TreeNode(StringBuilder str) { val = str; }
}
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
if (n < 1)
return new ArrayList<>();
StringBuilder s = new StringBuilder("(");
TreeNode tree = new TreeNode(s);
helper(tree,n*2);
return res;
}
public TreeNode helper(TreeNode tree,int n)
{
//判断是不是添加完了
StringBuilder temp = tree.val;
if (temp.length()>=n)
{
res.add(new String(temp));
return null;
}
//统计左右括号数
int l = 0;
int r = 0;
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i)=='(')
l++;
if (temp.charAt(i)==')')
r++;
}
//注意这里一定要新建,如果把temp直接赋给left和right的话,他们三个其实是指向同一个堆内存
StringBuilder left = new StringBuilder(temp);
StringBuilder right = new StringBuilder(temp);
left.append('(');
right.append(')');
//添加条件
if (r < l)
{
tree.right = helper(new TreeNode(right),n); }
if (l < n/2)
{
tree.left = helper(new TreeNode(left),n);
}
return tree;
}

22. Generate Parentheses生成指定个括号的更多相关文章

  1. [LeetCode] 22. Generate Parentheses 生成括号

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

  2. [leetcode]22. Generate Parentheses生成括号

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

  3. 22. Generate Parentheses产生所有匹配括号的方案

    [抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  4. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  5. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  6. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  7. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  8. [LintCode] Generate Parentheses 生成括号

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

  9. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

随机推荐

  1. 通过自定义拦截器优雅的导出Excel并标红的重复数据

    平时我们导入导出Excel的时候如果用poi导出,会发现光设置格式都要很多代码,看起来非常的不优雅.后来业务中遇到了需要导入非常巨大的Excel的需求.如果继续用poi的方式,因为poi把所有exce ...

  2. 转载的一篇文章eclipse添加插件

    eclipse没有(添加)"Dynamic Web Project"选项的方法 转载海边的第八只螃蟹 最后发布于2015-11-24 21:24:15 阅读数 40814  收藏 ...

  3. C++-codeblocks安装

    2020-02-15 "Test_leetcode - Debug": The compiler's setup (GNU GCC Compiler) is invalid, so ...

  4. java45

    Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); //移除集合中的某个元素 c2.remov ...

  5. 网络最大流 Dinic算法

    前言 看到网上好多都用的链式前向星,就我在用 \(vector\) 邻接表-- 定义 先来介绍一些相关的定义.(个人理解) 网络 一个网络是一张带权的有向图 \(G=(V,E)\) ,其中每任意一条边 ...

  6. Django----图片验证码接口

    1.django 缓存设置 django的六种缓存(mysql+redis) :https://www.cnblogs.com/xiaonq/p/7978402.html#i6 1.1 安装Djang ...

  7. SSM之Spring框架--->>墨盒案例

    分析: 程序中包括打印机(Printer).墨盒(Ink).和纸张(Paper).三类组件 首先创建一个新项目文件Spring_Box 我们来定义墨盒和纸张的接口类 墨盒接口Ink.java文件内容如 ...

  8. 20191012_WMI中可以看到有打印机, 设备管理器中没有

    开发过程中使用SELECT * FROM Win32_Printer 查看设备有一个备份打印机, 并且被设置为默认打印机了, 但是设备管理器中没有 解决方法: 使用系统自带的测试WMI  (Windo ...

  9. 如何使用TradingView(TV)回测数字货币交易策略

    更多精彩内容,欢迎关注公众号:数量技术宅.想要获取本期分享的完整策略代码,请加技术宅微信:sljsz01 TradingView平台简介 前段时间,有粉丝找到技术宅,表示他有一个常用的交易平台,叫做T ...

  10. App自动化《元素定位方式、元素操作、混合应用、分层设计、代码方式执行Pytest 命令》

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:oschina.云+社区.知乎等各大平台都有. 目录 一.App 元素定位方式 二.元素操作 三.测试混合应用 四.以代码的方式执行 Pytest 命 ...