码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 22. Generate Parentheses

时间:2019-08-04 19:39:29      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:技巧   medium   pair   solution   end   event   mtd   ide   return   

https://leetcode.com/problems/generate-parentheses/

Medium

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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

 

技术图片
 1 class Solution:
 2     def generateParenthesis(self, n: int) -> List[str]:
 3         if n == 0:
 4             return None
 5         
 6         result = []
 7         
 8         def helper(s = ‘‘, left = 0, right = 0):
 9             if len(s) == 2 * n:
10                 result.append(s)
11                 return
12             
13             if left < n:
14                 helper(s + (, left + 1, right)
15             
16             if right < left:
17                 helper(s + ), left, right + 1)
18         
19         helper()
20         return result        
View Python Code

 

Leetcode 22. Generate Parentheses

标签:技巧   medium   pair   solution   end   event   mtd   ide   return   

原文地址:https://www.cnblogs.com/pegasus923/p/11299186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!