[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 example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"

===Comments by Dabay===递归。用left和right来记录剩余的左右括号数量。如果都不剩余了,把结果放入要返回的列表中。如果剩下的左括号比右括号多,说明不是合法的组合,返回。‘‘‘

class Solution:    # @param an integer    # @return a list of string    def generateParenthesis(self, n):        def generateParenthesis2(left, right, string, res):            if left == 0 and right == 0:                res.append(string)                return            if left > right:                return            if left > 0:                generateParenthesis2(left-1, right, string + "(", res)            if right > 0:                generateParenthesis2(left, right-1, string + ")", res)

        res = []        generateParenthesis2(n, n, "", res)        return res

def main():    sol = Solution()    print sol.generateParenthesis(4)

if __name__ == ‘__main__‘:    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-12-23 07:40:51

[Leetcode][Python]22: Generate Parentheses的相关文章

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

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

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