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

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

有点像二叉树的建立过程

/*
思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号
判断好两种添加的条件后向后添加就行:
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. 手撕HashMap

    前言: 平时工作的时候,用的最多的就是ArrayList和HashMap了,今天看了遍HashMap的源码,决定自己手写一遍HashMap. 一.创建MyHashMap接口       我们首先创建一 ...

  2. Jmeter-BeanShell断言的运用二(不同Json格式的字段提取和断言判断)

    前言 为了更加熟悉BeanShell,所以用几个实例来记录说明下,不同的Json格式是怎么提取相应字段和判断断言的.(会持续更新...) 一.第一种Json格式 1.Json响应数据内容如下: { & ...

  3. 单调栈模板 POJ3250

    上次二分st表大法失败以后的又一次尝试233333 封装,封装,封装!!!!!! #include <bits/stdc++.h> using namespace std; #define ...

  4. Python+爬虫+xlwings发现CSDN个人博客热门文章

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 最近几天老猿博客的访问量出现了比较大的增长,从常规的1000-3000之间波动的范围一下子翻了将近一倍,粉丝增长从日均10-40人也增长了差不多一倍 ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象documentMode属性

    documentMode属性表示当前主窗口是否启用文档模式,如果是则主窗口的选项卡部件会以适合操作文档的模式呈现,这类似于macOS上的文档模式. 设置此属性时,界面上不会呈现选项卡部件框架.此模式当 ...

  6. MapReduce简单执行过程及Wordcount案例

    MapReducer运行过程 以单词统计为案例. 假如现在文件中存在如下内容: aa bb aa cc dd aa 当然,这是小文件,如果文件大小较大时会将文件进行 "切片" ,此 ...

  7. 3、pytorch实现最基础的MLP网络

    %matplotlib inline import numpy as np import torch from torch import nn import matplotlib.pyplot as ...

  8. Spark流式状态管理(updateStateByKey、mapWithState等)

    通常使用Spark的流式框架如Spark Streaming,做无状态的流式计算是非常方便的,仅需处理每个批次时间间隔内的数据即可,不需要关注之前的数据,这是建立在业务需求对批次之间的数据没有联系的基 ...

  9. 团队作业4-Day2

    团队作业4-Day2 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图(部分) 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日进行了小程序与网页代码编写的区 ...

  10. 一个最简单的typescript工程

    初级: 1.新建一个文件夹~/a/ 2.cd ~/a/ 3.npm init -y          生成package.json 4.新建index.ts,内容:console.log(" ...