【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 ...
随机推荐
- Bootstrap 按钮下拉菜单
向下拉 <div class="dropdown"> <button class="btn btn-default" data-toggle= ...
- Android Studio 打包自定义apk文件名
使用Android Studio打包的时候,我们有时候需要自定义apk的文件名,在此记录一下. 在app的build.gradle中,根节点下使用关键词def声明一个全局变量,用于获取打包的时间,格式 ...
- WPF技术点
常用Path路径 正三角形(左):<Path Data="M40,0 L0,30 40,60 z" Stretch="Uniform"/> 正三角形 ...
- amcharts的一些用法
function chartdiv2() { var chart; var chartData = [ { "month" : "2015-08", " ...
- c++ virtual总结
virtual-关键字用于修饰成员函数时,有以下特性 1.用于修饰的基类的成员函数,被修饰的基类成员函数-其派生类的同名成员函数也默认带有virtual 关键字2.当virtual 用于修饰析构函数( ...
- 5、Linux操作系统介绍
1操作系统的作用·是现代计算机系统中最基本和最重要的系统软件·是配置在计算机硬件上的第一层软件,是对硬件系统的首次扩展·主要作用是管理好硬件设备,并为用户和应用程序提供一个简单的接口,以便于使用·而其 ...
- git创建新分支推送到远程
1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义. 2.切换本地分支 git ch ...
- html中插入其他html,并实现动态效果!
<html> <body> 倒计时开始...... <span id="s1">888</span> <!--在html中先做 ...
- hdu 5373 The shortest problem(杭电多校赛第七场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...
- [005] unique_sub_string
[Description] Given a string, find the largest unique substring. e.g. str[] = "asdfghjkkjhgf&qu ...