Leetcode 22. Generate Parentheses
Posted pegasus923
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 22. Generate Parentheses相关的知识,希望对你有一定的参考价值。
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:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
- 回溯,DFS。学习判断括号有效的剪枝技巧。
- https://leetcode.com/problems/generate-parentheses/solution/
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
以上是关于Leetcode 22. Generate Parentheses的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 22. Generate Parentheses
leetcode22. Generate Parentheses
leetcode 22 -- Generate Parentheses
[leetcode-22-Generate Parentheses]