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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

给定一个整数,输出所有正确匹配的括号的组合。

二、思路递归,递归我并不了解很深,只是知道什么时候能用递归。递归有三个特性:1、必须有一个明确的结束条件;2、每次递归都是为了让问题规模变小;3、递归层次过多会导致栈溢出,且效率不高
三、代码
#coding:utf-8
class Solution(object):
    def func(self, array, string, left, right):
        """array:最终生成的list
           string:本轮已经完成的str
           left:左括号剩余个数
           right:右括号剩余个数
        """
        if left == 0 and right == 0:  # 递归终止条件,左边括号与右边括号剩余个数均为0
            array += [string]
        if left > 0:  # 左边括号剩余大于0,则可以继续往下递归
            self.func(array, string + "(", left - 1, right)
        if right > 0 and left < right:  # 右边括号剩余大于0且左边剩余小于右边剩余,则可继续递归
            self.func(array, string + ")", left, right - 1)

    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        rtn = []
        self.func(rtn, "", n, n)
        print(rtn)
        return rtn

"""
递归例子
"""
def division(n):
    print(n)
    if int(n/2)==0:
        return n#递归特性一:必须有一个明确的结束条件
    return division(int(n/2))#递归特性二:每次递归都是为了让问题规模变小

division(10)#递归特性三:递归层次过多会导致栈溢出,且效率不高

if __name__ == ‘__main__‘:
    ss = Solution()
    ss.generateParenthesis(2)

  

原文地址:https://www.cnblogs.com/xiaodongsuibi/p/8903390.html

时间: 2024-09-29 20:59:02

LeetCode Medium:22. Generate Parentheses的相关文章

[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

一天一道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】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

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

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

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

[LeetCode] 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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

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