[leedcode 22] Generate Parentheses

public class Solution {
    //卡特兰数,一共有C2n^n-C2n^n-1种组合数
    //本题的递归非常经典,需要多看牢记
    List<String> res;
    StringBuilder seq;
    public List<String> generateParenthesis(int n) {
       res=new ArrayList<String>();
       seq=new StringBuilder();
       generate(n,0,0,0);
       return res;
    }
    public void generate(int n,int left,int right,int level){
        if(2*n==level){
            res.add(seq.toString());
            return;
        }
        if(left<n){
            seq.append(‘(‘);
            generate(n,left+1,right,level+1);
            seq.deleteCharAt(level);//记得要删除

        }
        if(left>right){
            seq.append(‘)‘);
            generate(n,left,right+1,level+1);
           seq.deleteCharAt(level);

        }

    }
}
时间: 2024-08-28 00:41:32

[leedcode 22] Generate Parentheses的相关文章

No.22 Generate Parentheses

No.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: "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses(js)

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: [ "((()))", "(()())", "(())()", "()(())",

22. Generate Parentheses dfs填表

22. Generate Parentheses Medium 2421147FavoriteShare 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 22.Generate Parentheses (生成括号) 解题思路和方法

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】22. Generate Parentheses

题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 解题分析: 这类题一般都要用递归的方法来解决.需要设两个集合类分别存储待匹配的(,)的个数. 这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误.(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果. 具体代

#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: "((()))", "(()())", "(())()", "()(())", "()()()" 本题是括号匹配

22. Generate Parentheses——本质:树,DFS求解可能的path

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: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] cla

【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: [ "((()))", "