zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    UVALive 7146 (贪心+少许数据结构基础)2014acm/icpc区域赛上海站
    hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场
    【进阶——树状数组】 区间求最值
    软件测试作业4
    软件测试作业三 尝试使用JUnit
    软件测试作业2:读代码回答问题
    软件测试作业1:描述一个曾遇到的错误
    hdu1064Financial Management
    hdu1161Eddy's mistakes
    hdu1020Encoding
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9751745.html
Copyright © 2011-2022 走看看