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:

[

"((()))",

"(()())",

"(())()",

"()(())",

"()()()"]

Solution1:(TLE)

import itertools
class Solution:
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        def judge(s):
            count = 0
            for i in s:
                if i==‘(‘:
                    count += 1
                if i==‘)‘:
                    count -= 1
                    if count<0:
                        return False
            return True
        s = ‘()‘*n
        res = set()
        for i in itertools.permutations(s,2*n):
            temp = ‘‘.join(i)
            if judge(temp):
                res.add(temp)
        return list(res)

生成所有全排列,逐个判断是否合理。然后在n=5的时候就炸了。

Solution2:

class Solution:
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        def judge(s):
            count = 0
            for i in s:
                if i==‘(‘:
                    count += 1
                if i==‘)‘:
                    count -= 1
                    if count<0:
                        return False
            if count ==0:
                return True
            else:
                return False
        res = []
        def generate(A = []):
            if len(A)==n+n:
                temp = ‘‘.join(A)
                if judge(temp):
                    res.append(temp)
            else:
                A.append(‘(‘)
                generate(A)
                A.pop()
                A.append(‘)‘)
                generate(A)
                A.pop()
        generate()
        return res

同样是生成所有全排列,但是这里思路是分别在每个位置上放‘(’ 和‘)‘,减少了很多的搜索空间。这种递归生成的方式值得注意。

Solution3:

class Solution:
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        def judge(s):
            count = 0
            for i in s:
                if i==‘(‘:
                    count += 1
                if i==‘)‘:
                    count -= 1
                    if count<0:
                        return False
            if count ==0:
                return True
            else:
                return False
        res = []
        def generate(num):
            temp = ‘0‘*(2*n-len(str(num))) + str(num)
            s = ‘‘
            for i in temp:
                if i==‘0‘:
                    s += ‘(‘
                else:
                    s += ‘)‘
            return s
        for i in range(2**(2*n)):
            t = generate(bin(i).split(‘b‘)[-1])
            if judge(t):
                res.append(t)
        return res

思路同solution2,思路是每个位置都有两个候选,那么可以用一个二进制数去标记它。所有的空间也就是在2的2n次方的这个范围里。

Solution4:

class Solution:
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        res = []
        def generate(a,left,right):
            if len(a)==2*n:
                res.append(‘‘.join(a))
                return
            if left<n:
                generate(a+‘(‘,left+1,right)
            if right<left:
                generate(a+‘)‘,left,right+1)
        generate(‘‘,0,0)
        return res

回溯。不是先生成所有组合,而是用递归回溯的方法生成所有合法的组合。

已知左括号先出现,且个数为0~n,因此先对左括号进行递归到底,条件为left < n(用left记录左括号数)。然后对右括号递归,条件为右括号数(用right记录)比左括号数少,即right < left。

递归出口为生成的串A长度增长到2n时。

参考:https://blog.csdn.net/u012033124/article/details/80532460

原文地址:https://www.cnblogs.com/bernieloveslife/p/9751745.html

时间: 2024-10-19 11:23:00

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. 解题分析: 这类题一般都要用递归的方法来解决.需要设两个集合类分别存储待匹配的(,)的个数. 这里需要明白一点:当待匹配的(的个数永远不小于待匹配的)的个数时只能匹配(,否则会导致错误.(可以自己在纸上试一下就好理解了),其余情况可以考虑匹配( 和)两种情况下可能的结果. 具体代

[LeetCode] 22. Generate Parentheses Java

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

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

[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,