22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边)
思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号
有点像二叉树的建立过程
/*
思路是从第一个符号开始添加,只有两种情况,一种是添加左括号,一种是添加右括号
判断好两种添加的条件后向后添加就行:
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生成指定个括号的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses产生所有匹配括号的方案
[抄题]: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- Python的偏函数
import functools def showag(*args,**kwargs): print(args) print(kwargs) p1 = functools.partial(showag ...
- web服务器专题:tomcat基础及模块
Web服务器专题:Tomcat(一)基础架构 针对java系的经典服务器,打算系统的整理一下Tomcat的机制和一些原理,以此记录. 插一则题外话,关于tomat这个名字的由来:Tomcat 名称的由 ...
- MySQL(13)---MYSQL主从复制原理
MYSQL主从复制原理 最近在做项目的时候,因为部署了 MYSQL主从复制 所以在这里记录下整个过程.这里一共会分两篇博客来写: 1.Mysql主从复制原理 2.docker部署Mysql主从复制实战 ...
- Netty 心跳处理
传统的心跳包设计,基本上是服务端和客户端同时维护 Scheduler,然后双方互相接收心跳包信息,然后重置双方的上下线状态表.此种心跳方式的设计,可以选择在主线程上进行,也可以选择在心跳线程中进行,由 ...
- moviepy音视频剪辑基类VideoClip的write_gif方法opt、fuzz、dispose、colors、loop等参数的作用
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类write_gif方法用于将视频剪辑输出到gif文件,调用语法如下: def write_gif(self, fil ...
- Eclipse配置反编译
Eclipse配置反编译 之前用IDEA一直让我很喜欢的点就是,什么东西都自动集成,下载.但是终归是学(po)习(jie)版,在正式企业开发中,要小心版权的问题(公司给你买了当我没说).抛开插件能 ...
- sklearn决策树应用及可视化
from sklearn import datasets from sklearn.tree import DecisionTreeClassifier 1.载入iris数据集(from sklear ...
- 理解js浅拷贝和深拷贝
理解深拷贝和浅拷贝之前先了解下js中的基本类型和引用类型 1.基本类型: 在js中,数据的基本类型undefined,null,string,number,boolean,在变量中赋的实际值,基本类型 ...
- CSP-S 2020 题解
赛后我重拳出击,赛场上我却爆零.哎. 题解本人口胡.有错请各位大佬们指出. A. 儒略日 这题是大型模拟题. 介绍两种写法:一种代码量致死(赛 场 自 闭),一种是非常好写的. 写法 1 我在赛场的思 ...
- Kubernetes Python Client 初体验之安装授权
最近想做一个基于flask的云平台管理服务器,利用python调用kubenetes提供的API来实现云平台的操作.笔者使用的是Windows,kubernetes集群安装在Ubuntu和Respbi ...