Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"
思路:采用递归的思想,当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。不过,每一个方法的调用都会产生一个栈帧,每执行一个方法就会出现压栈操作,所以采用递归的时候产生的栈帧比较多,递归就会影响到内存,非常消耗内存。当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。
public static void main(String[] args){
ArrayList<String> res = new ArrayList<String>();
Scanner input = new Scanner(System.in);
System.out.println("请输入n:");
int n = input.nextInt();
generate(res, "", 0, 0, n);
System.out.println(res);
}
public static void generate(ArrayList<String> res, String tmp, int lhs, int rhs, int n){
if(lhs == n){
for(int i = 0; i < n - rhs; i++){
tmp += ")";
}
res.add(tmp);
return ;
}
generate(res, tmp + "(", lhs + 1, rhs, n);
if(lhs > rhs)
generate(res, tmp + ")", lhs, rhs + 1, n);
}
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()"的更多相关文章
- [Javascript] Write a Generator Function to Generate the Alphabet / Numbers
When you need to generate a data set, a generator function is often the correct solution. A generato ...
- [LeetCode] 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 ...
- 【leetcode】Generate Parentheses
题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- No.022:Generate Parentheses
问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...
- Generate Parentheses
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【leetcode】 Generate Parentheses (middle)☆
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- mybatis if test 解决页面 升序和降序的问题
<if test="status !=null and status !='' and status =='1'.toString()"> ORDER BY tag.c ...
- asp.net使用qq邮箱发送邮件
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- Linux系统网卡设置
由于做了虚拟机的克隆,发现克隆机和被克隆机的MAC地址相同了,下面我将要介绍一下linux中网卡的配置步骤,我使用的linux是CentOS release 6.9 (Final) 1.root用户编 ...
- Image和字节数组互转
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- 搭建nexus私服(maven)
这里提供nexus的直接下载页面的链接: https://www.sonatype.com/download-oss-sonatype maven获取依赖jar包是从中央仓库获取,但很莫名的出现jar ...
- Linux 安装依赖库
###安装依赖库###yum -y install rsync net-snmp syslog net-snmp-devel wget patch screen gcc gcc-c++ autocon ...
- FileEditor
package JBJADV003; import java.io.*; class FileEditor { public static void main(String args[]) throw ...
- 实时音视频互动系列(上):又拍云UTUN网络详解
如何定义实时音视频互动, 延迟 400ms 内才能无异步感 实时音视频互动如果存在1秒左右的延时会给交流者带来异步感,必须将视频播放延迟限制在400ms以内,才能给用户较好的交互体验. 当延迟控制在4 ...
- Java缓存框架使用EhCache结合Spring AOP
一.Ehcache简介 EhCache是一个纯Java的进程内缓存框架,具有如下特点: 1. 快速简单,非常容易和应用集成. 2.支持多种缓存策略 . 3. 缓存数据有 ...
- Jersey +jetty 实现微服务(一)
微服务: 传输协议基本都是http,数据序列化,协议层目前protocol buffer,Json, xml,Java序列化,最多使用Json.实现以上二点并不难,spring 就可以,但spr ...