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 ...
随机推荐
- poi 1182
食物链 || 带权并查集 0:同类 1:吃 2:被吃 #include <cstdio> using namespace std; const int maxn=5e4+3; int f[ ...
- 安装IAR编译器详解
1.首先下载好安装包和破解包 我安装使用的版本:IAR for 8051 v9.10 链接: https://pan.baidu.com/s/13x36j5qL90YokrAlyChQhw 提取码: ...
- DRF的ModelSerializer的使用
在views中添加 from django.shortcuts import render # Create your views here. from rest_framework.views im ...
- 零钱问题的动态规划解法——用 n 种不同币值的硬币凑出 m 元,最少需要多少硬币。
输入格式:第一行输入需要凑的钱数 m 和硬币的种类 n (0<m<100,0<n<10),第二行输入 n 种硬币的具体币值,假设硬币供应量无限多. 输出格式:输出最少需要的硬币 ...
- springboot多模块项目搭建遇到的问题记录
废话不多说,直接上问题报错与解决方法. 问题报错一:(报错信息看下方代码) 问题原因:'com.company.logistics.service.company.CompanyService' 未找 ...
- Python中的迭代是什么意思?
Python中的迭代是指通过重复执行的代码处理相似的数据集的过程,并且本次迭代的处理数据要依赖上一次的结果继续往下做,上一次产生的结果为下一次产生结果的初始状态,如果中途有任何停顿,都不能算是迭代. ...
- 第15.37节 PyQt(Python+Qt)入门学习:containers容器类部件QMdiArea多文档界面部件详解及编程开发案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 老猿在前期学习PyQt相关知识时,对每个组件的属性及方法都研 ...
- windows+jenkins+iis 部署
1.安装jenkins 下载地址:https://www.jenkins.io/download/ 2.需要配置java环境 配置教程:https://www.cnblogs.com/liuxiaoj ...
- MySQL入门看这一篇就够了
MySQL JavaEE:企业级Java开发 web阶段 分为1.前端(页面,展示数据库中的数据) 2.后台(连接点:链接数据库JDBC.Mybatis,链接前端(控制视图跳转,给前端传递数据)) 3 ...
- 支付宝小程序input的小坑
//axml<input class="internet_input" value="{{payNo}}" onInput="keyNum&qu ...