【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 ...
随机推荐
- [DeeplearningAI笔记]卷积神经网络3.1-3.5目标定位/特征点检测/目标检测/滑动窗口的卷积神经网络实现/YOLO算法
4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.1目标定位 对象定位localization和目标检测detection 判断图像中的对象是不是汽车--Image clas ...
- 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Anagrams by Stack(深度优先搜索)
ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds Memory Limit: 65536 KB How can a ...
- IE6+IE7+IE8+IE9+FF兼容性调试
HACK原理:不同浏览器对各中字符的识别不同 (读完文章你会发现,FF和IE8对以下字符的识别能力完全相同) 在 CSS中常用特殊字符识别表: (1)*: IE6+IE7都能识别*,而标准浏览器FF ...
- virtual和abstract的区别和联系
壹. 相同 他们有些相似.有些场景用哪个都行! 1. 修饰父类.让子类重写 virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义. 2. 都用必须public 如 ...
- MongoDB中的基础概念:Databases、Collections、Documents
MongoDB以BSON格式的文档(Documents)形式存储.Databases中包含集合(Collections),集合(Collections)中存储文档(Documents). BSON是一 ...
- 【译】msfvenom
原文链接:MSFvenom 1.使用MSFvenom命令行界面 msfvenom是Msfpayload和Msfencode的组合,将这两个工具集成在一个框架实例中. msfvenom的优点是: 一个单 ...
- jquery.cookie.js——jquery的cookie插件
一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...
- 使用迭代法穷举1到N位最大的数
这是何海涛老师剑指offer上面第12题,这题首先注意不能使用整数int型作为操作对象,因为N很大时明显会溢出.这种大数据一般都是使用的字符串来表示. 直接法就是:1.针对字符串的加法,涉及循环进位及 ...
- Java network programming-guessing game
猜数字游戏 游戏的规则如下: 当客户端第一次连接到服务器端时,服务器端生产一个[0,50]之间的随机数字,然后客户端输入数字来猜该数字,每次客户端输入数字以后,发送给服务器端,服务器端判断该客户端发送 ...