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

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

有点像二叉树的建立过程

/*
思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号
判断好两种添加的条件后向后添加就行:
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. 04-Python里字符串的常用操作方法三-判断

    1. startswith(): 判断字符串是否以某个子串开始,是则返回True,否则返回False 示例: my_str = 'hello world and my and test and pyt ...

  2. 05 python开发之文件处理

    05 python开发之文件处理 目录 05 python开发之文件处理 5 文件处理 5.1 字符编码 5.1.1 基本概念 5.1.2 发展历程 5.1.3 使用 5.2 文件处理基础 5.2.1 ...

  3. 单调栈模板 POJ3250

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

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

    animated属性用于设置在操作可浮动部件和工具栏时是否设置动画. 当一个可浮动部件或工具栏被拖到主窗口上时,主窗口将调整其内容,以显示浮动部件或工具栏应该放置的位置.设置此属性后主窗口将使用平滑动 ...

  5. HTTP助记

    1** 信息,服务器收到请求,需要请求者继续执行操作 100 continue 继续,客户端应继续请求 101 swithching protocls 切换协议,服务器根据客户端的请求切换协议.只能切 ...

  6. 【题解】AcWing 193. 算乘方的牛

    原题链接 题目描述 约翰的奶牛希望能够非常快速地计算一个数字的整数幂P(1 <= P <= 20,000)是多少,这需要你的帮助. 在它们计算得到最终结果的过程中只能保留两个工作变量用于中 ...

  7. 关于C++的异常抛出

    在接触 throw 之前,我们只知道可以通过函数的返回值来获取和定位错误,比如通过 return 来层层返回是一种方法,但如果牵扯到多层函数调用,那么通过 return 来返回错误显得过于拖沓,这时就 ...

  8. typora软件使用指南

    Markdown学习 标题: 三级标题 四级标题 字体 hello,world! hello,world! hello,world! hello,world! 引用 选择狂神说java,走向人生巅峰 ...

  9. 2020-2021-1 20209307 《Linux内核原理与分析》第十一周作业

    这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)> 这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第十一周作业> 这个作业的目标 ...

  10. 死磕以太坊源码分析之Ethash共识算法

    死磕以太坊源码分析之Ethash共识算法 代码分支:https://github.com/ethereum/go-ethereum/tree/v1.9.9 引言 目前以太坊中有两个共识算法的实现:cl ...