22. Generate Parentheses

原创 2018年04月15日 21:16:10

题目

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=N, 每一个可行的解都可以由一对”(“、”)”拆分为两个n=i、n=N-i-1的可行解,解即为n=i,n=N-i-1的量量组合加上”(“、”)”。当n=1时,解为‘()’;当n=0时,解为空。因此可以写成递归的解法。
利用动态规划思路,采用dp[i],存储n=i的解,可用空间换取时间,避免多次求解n=i的解。

递归解法

from itertools import product
def my_help(n):
    if n < 1: return [[]]
    if n == 1: return [['(',')']]
    output = []
    for i in range(1, 2*n, 2):
        for tem_a, tem_b in list(product(my_help(i//2), my_help((2*n - i - 1)//2))):
            output.append(['('] + tem_a + [')'] + tem_b)
    return output
class Solution(object):
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        return [''.join(item) for item in my_help(n)]

动态规划解法

class Solution(object):
    def generateParenthesis(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        dp = [[] for i in range(n + 1)]
        dp[0].append('')
        for i in range(n + 1):
            for j in range(i):
                dp[i] += ['(' + x + ')' + y for x in dp[j] for y in dp[i - j - 1]]
        return dp[n]

LeetCode 22 — Generate Parentheses(C++ Java Python)

题目:http://oj.leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a func...
  • dragon_dream
  • dragon_dream
  • 2014-04-17 11:11:41
  • 3160

LeetCode 22 Generate Parentheses (C,C++,Java,Python)

Problem: Given n pairs of parentheses, write a function to generate all combinations of well-fo...
  • runningtortoises
  • runningtortoises
  • 2015-05-10 21:11:15
  • 1486

LeetCode 22:Generate Parentheses的递归,回溯两种解法

Generate Parentheses Given n pairs of parentheses, write a function to generate all combination...
  • liuyuan185442111
  • liuyuan185442111
  • 2015-08-24 11:29:56
  • 750

LeetCode(22)Generate Parentheses

题目Given n pairs of parentheses, write a function to generate all combinations of well-formed parenth...
  • fly_yr
  • fly_yr
  • 2015-09-26 19:57:28
  • 1901

LeetCode --- 22. Generate Parentheses

题目链接:Generate Parentheses Given n pairs of parentheses, write a function to generate all combinatio...
  • makuiyu
  • makuiyu
  • 2015-01-31 13:08:39
  • 2600

LeetCode 22. Generate Parentheses(生成括号)

原题网址:https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a fun...
  • jmspan
  • jmspan
  • 2016-05-19 07:18:56
  • 337

[Java]LeetCode22 Generate Parentheses

[Java]LeetCode22 Generate Parentheses
  • fumier
  • fumier
  • 2015-04-23 09:55:04
  • 655

LeetCode 22 Generate Parentheses(生成括号)

翻译给定一个括号序列,写一个函数用于生成正确形式的括号组合。 例如,给定n = 3,一个解决方案集是: "((()))", "(()())", "(())()", "()(())", "()()()"...
  • NoMasp
  • NoMasp
  • 2015-11-10 22:41:54
  • 4824

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

Generate Parentheses  Given n pairs of parentheses, write a function to generate all combinatio...
  • xygy8860
  • xygy8860
  • 2015-07-06 22:11:08
  • 859

leetcode 22. Generate Parentheses-回溯|递归

原题链接:22. Generate Parentheses 【思路-Java、Python】 递归实现 采用递归方式,添加左括号的条件是左括号数量小于 n,添加右括号的条件是右括号数小于左括号数:...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-05-28 16:27:19
  • 689
收藏助手
不良信息举报
您举报文章:22. Generate Parentheses
举报原因:
原因补充:

(最多只允许输入30个字)