【leetcode】22. Generate Parentheses
题目描述:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
解题分析:
这类题一般都要用递归的方法来解决。需要设两个集合类分别存储待匹配的(,)的个数。
这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误。(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果。
具体代码:
public class Solution {
public static List<String> generateParenthesis(int n){
List<String> result = new ArrayList<String>();
List<Character> array1=new LinkedList<Character>();
List<Character> array2=new LinkedList<Character>();
char[] array = new char[2*n];
for(int i=0;i<n;i++){
array1.add('(');
array2.add(')');
}
fun1(array1,array2,result,array,0);
return result;
}
public static void fun1(List<Character> array1,List<Character> array2,List<String> result,char[] array,int index){
if(index==array.length-1){
if(array1.size()==0&&array2.size()==1){
array[index]=array2.remove(0);
result.add(new String(array));
array[index]=' ';
array2.add(')');
return;
}
else{
return;
}
}
//只能填'('
if(array1.size()>=array2.size()){
array[index]=array1.remove(0);
fun1(array1,array2,result,array,index+1);
array[index]=' ';
array1.add('(');
}
else{
//先试'('
if(array1.size()>0){ array[index]=array1.remove(0);
fun1(array1,array2,result,array,index+1);
array[index]=' ';
array1.add('(');
}
//再试')'
array[index]=array2.remove(0);
fun1(array1,array2,result,array,index+1);
array[index]=' ';
array2.add(')');
} }
}
【leetcode】22. Generate Parentheses的更多相关文章
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】22. Generate Parentheses (I thought I know Python...)
I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- 【一天一道LeetCode】#22. Generate Parentheses
一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of we ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- LeetCode OJ 22. Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode】478. Generate Random Point in a Circle 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/generate ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 基于javaWeb阶段下的Servlet总结
1. Servlet概述 Servlet是用Java语言编写的服务端的程序,采用request--response模式提供Web服务,并且支持标准ServletAPI,Servlet就一个运行在w ...
- React读取Excel——js-xlsx 插件的使用
介绍 SheetJS js-xlsx 是一款能够读写多种格式表格的插件,浏览器支持良好,并且能在多个语言平台上使用,目前在 github 上有 12602 个 star, 刚好项目中遇到了前端解析 e ...
- Linux 下 JDK + Eclipse + PyDev 安装与配置
一:JDK / JRE 环境 Eclipse 是运行于Java虚拟机中的,所以必须先安装Java环境才能进行开发测试.JRE(Java Runtime Environment)是运行环境,JDK(Ja ...
- Oracle用imp导入dmp 提示遇到 ORACLE 错误 12560 TNS: 协议适配器错误 解决方法
用imp命令导入dmp文件时提示以下错误: IMP-00058: 遇到 ORACLE 错误 12560 : ORA-12560: TNS: 协议适配器错误 : IMP-00000: 未成功终止导入 : ...
- HNOI2004 宠物收养所 (Treap)
1285 宠物收养所 http://codevs.cn/problem/1285/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 最近,阿Q开了一间 ...
- 【洛谷 P2512】 [HAOI2008]糖果传递(贪心)
题目链接 环形均分纸牌. 设平均数为\(ave\),\(g[i]=a[i]-ave\),\(s[i]=\sum_{j=1}^ig[i]\). 设\(s\)的中位数为\(s[k]\),则答案为\(\su ...
- Sqlmap注入技巧收集整理
TIP1 当我们注射的时候,判断注入 http://site/script?id=10http://site/script?id=11-1 # 相当于 id=10http://site/script? ...
- sqlmap的使用方法 ——时光凉春衫薄
普通注入 Sqlmap -u “http://www.xxxxxx.com/xxxx/xxx/xxx.xxx?xx=xx” --dbs 找到一个sql的注入点 探测他的库名 access的直接探表 ...
- SPI协议及其工作原理浅析【转】
转自:http://www.laoliu-soft.net/category/tech_chap/tech_linux/ 一.概述. SPI, Serial Perripheral Interface ...
- 【IT公司笔试面试】75道逻辑推理题及答案
[1]假设有一个池塘,里面有无穷多的水.现有2个空水壶,容积分别为5升和6升.问题是如何只用这2个水壶从池塘里取得3升的水. 由满6向空5倒,剩1升,把这1升倒5里,然后6剩满,倒5里面,由于5里面有 ...