【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 ...
随机推荐
- [实战篇]Tomcat发布项目-虚拟目录
在二阶段学习的过程中,我一直使用MyEclipse的方式把工作空间的项目发布到webapps目录下,这种方式自我感觉在实际开发中应用能在70%左右,但是如何涉及到一些上传操作等操作, 从新发布项目之后 ...
- php实现多继承-trait语法
自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制,使开发人员能 ...
- LightOJ 1096 - nth Term 矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n) = a * f(n-1) + b * f(n-3) + c, ...
- 更改gradle的java的class文件输出目录的结构
group 'com.thinkvenus.common'version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 r ...
- 如何在移动端app中应用字体图标icon fonts
How to use icon fonts in your mobile apps 在任何APP设计中实现可图形的矢量缩放最完美的方式是使用字体图标. 移动端的设计变的越来越复杂.原因在于多样的屏幕尺 ...
- c语言学习笔记.条件编译.#if,#ifdef,if的区别
最近遇到了,以此做个记录. 条件编译 是C预处理部分的内容. 其判断语句包括 #if #else if #else 以及 #ifdef 和 #endif. 使用 #if (表达式) codes1. ...
- sqlmap tamper编写
#!/usr/bin/env python """ Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.or ...
- 数据库与sql注入的相关知识
数据库与sql注入的相关知识 sql语句明显是针对数据库的一种操作,既然想通过sql注入的方法来拿取数据那么就要先了解一下如何的去操作数据库,这方面并不需要对数据库有多么的精通但是如果了解掌握了其中的 ...
- React 16 源码瞎几把解读 【三 点 二】 react中的fiberRoot
〇.先来看看常用的常量 NoWork = 0 noTimeout = undefined HostRoot = 3 NoContext = 0b000; AsyncMode = 0b001; Stri ...
- ProxySQL 监控和统计
ProxySQL 监控和统计 很多有价值的统计数据在stats和monitor库中. admin@127.0.0.1 [(none)]>SHOW TABLES FROM stats; +---- ...