【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 ...
随机推荐
- PlantUML类图
PlantUML类图 雨客 2016-04-08 11:38:03 浏览796 评论0 摘要: 类之间的关系 PlantUML用下面的符号来表示类之间的关系: 泛化,Generalization: ...
- [LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。
LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stat ...
- PHP字符串定义方式和单引号双引号的区别
$str=<<<任意标记 ......................... .............................. 任何标记; 单引号和双引号的区别? ''号 ...
- HTML标签marquee 来制作页面滚动
页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用m ...
- codeforces 872E. Points, Lines and Ready-made Titles
http://codeforces.com/contest/872/problem/E E. Points, Lines and Ready-made Titles time limit per te ...
- [USACO14JAN]Recording the Moolympics
题目描述 Being a fan of all cold-weather sports (especially those involving cows), Farmer John wants to ...
- bzoj 3678 wangxz与OJ
3678: wangxz与OJ Time Limit: 10 Sec Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.php ...
- Eng1—English daily notes
English daily notes 2015年 4月 Phrases 1. As a side note #作为附注,顺便说句题外话,和by the way意思相近,例句: @1:As a sid ...
- Mysql储存过程5: while
循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() be ...
- 64_o1
OCE-devel-0.18.1-1.fc26.i686.rpm 15-May-2017 18:37 5634474 OCE-devel-0.18.1-1.fc26.x86_64.rpm 15-May ...