21. Generate Parentheses Leetcode Python

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

看到这道题目首先想到的是catlan Number  根据catlan number我们可以知道最后的解的数量是catlan number

这里要求我们求解我们可以用bfs枚举来解决。

1.当左括号数量小于n 时候添加左括号

2.当有括号数量小于左括号时候添加右括号

3.当总括号长度等于2n时候将解加入解的集合。

this is a catlan number problem. we can know the final solution length is the corresponding catlan number.

to give specific solutions

1. when # of left parentheses is no greater than n we append left parentheses

2. when # of right parentheses is not greater than left parentheses we append right parentheses

3. when the total length is 2n we append it to solution

code is as follow:

class Solution:
    # @param an integer
    # @return a list of string
    def findp(self,valuelist,solution,n,left,right):
        if len(valuelist)==2*n:
            solution.append(valuelist)
        if left<n:
            self.findp(valuelist+'(',solution,n,left+1,right)
        if right<left:
            self.findp(valuelist+')',solution,n,left,right+1)
    def generateParenthesis(self, n):
        solution=[]
        self.findp('',solution,n,0,0)
        return solution
        
时间: 2024-11-13 09:39:11

21. Generate Parentheses Leetcode Python的相关文章

Generate Parentheses leetcode 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: "((()))", "(()())", "(())()", "()(())", "()()()" 题解:

LeetCode 21 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: "((()))", "(()())", "(())()", "()(())", "()()()" 思路:假设当前

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: "((()))", "(()())", "(())()", "()(())", "()()()" 思路:我们通过

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: "((()))", "(()())", "(())()", "()(())", "()()()" 题目大意:给一

Longest Valid Parentheses @Leetcode -- Python

http://oj.leetcode.com/problems/longest-valid-parentheses/ 1 class Solution: 2 # @param s, a string 3 # @return an integer 4 def longestValidParentheses(self, s): 5 if s == '' or s == '(' or s == ')': 6 return 0 7 stack = [(-1, ')')] 8 maxLen = 0 9 f

Generate Parentheses 【python】

午睡醒敲到3点钟,搞了一个多小时. 应该是一种 非递归 的实现方法吧. 例如,4个括号的情况,可以看成是: (3个括号的情况 连接上 1个括号的情况)+(2个括号的情况 连接上 2个括号的情况)+(1个括号的情况 连接上 3个括号的情况) + (‘(’ 连接上 3个括号的情况 连接上 ‘)’): 只是这样会每次产生重复的项,要先去掉,才能给下一次调用使用. class Solution: # @param an integer # @return a list of string def gen

[LeetCode][JavaScript]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: 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】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"