【leetcode】22. Generate Parentheses

题目描述:

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

解题分析:

这类题一般都要用递归的方法来解决。需要设两个集合类分别存储待匹配的(,)的个数。

这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误。(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果。

具体代码:

 1 public class Solution {
 2     public static List<String> generateParenthesis(int n){
 3         List<String> result = new ArrayList<String>();
 4         List<Character> array1=new LinkedList<Character>();
 5         List<Character> array2=new LinkedList<Character>();
 6         char[] array = new char[2*n];
 7         for(int i=0;i<n;i++){
 8             array1.add(‘(‘);
 9             array2.add(‘)‘);
10         }
11         fun1(array1,array2,result,array,0);
12         return result;
13     }
14     public static void fun1(List<Character> array1,List<Character> array2,List<String> result,char[] array,int index){
15         if(index==array.length-1){
16             if(array1.size()==0&&array2.size()==1){
17                 array[index]=array2.remove(0);
18                 result.add(new String(array));
19                 array[index]=‘ ‘;
20                 array2.add(‘)‘);
21                 return;
22             }
23             else{
24                 return;
25             }
26         }
27         //只能填‘(‘
28         if(array1.size()>=array2.size()){
29             array[index]=array1.remove(0);
30             fun1(array1,array2,result,array,index+1);
31             array[index]=‘ ‘;
32             array1.add(‘(‘);
33         }
34         else{
35             //先试‘(‘
36             if(array1.size()>0){
37
38                 array[index]=array1.remove(0);
39                 fun1(array1,array2,result,array,index+1);
40                 array[index]=‘ ‘;
41                 array1.add(‘(‘);
42             }
43             //再试‘)‘
44             array[index]=array2.remove(0);
45             fun1(array1,array2,result,array,index+1);
46             array[index]=‘ ‘;
47             array2.add(‘)‘);
48         }
49
50     }
51 }
时间: 2024-10-13 00:55:29

【leetcode】22. Generate Parentheses的相关文章

【LeetCode】22. Generate Parentheses (I thought I know Python...)

I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "

【一天一道LeetCode】#22. Generate Parentheses

一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "(

【LeetCode】022. Generate Parentheses

题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]

[Leetcode][Python]22: Generate Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 22: Generate Parentheseshttps://oj.leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For exampl

【LeetCode】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【leetcode】Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

【Leetcode】Remove Invalid Parentheses

题目链接:https://leetcode.com/problems/remove-invalid-parentheses/ 题目: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parent

LeetCode Medium:22. Generate Parentheses

一.题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()"

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=