File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3
+
4
+ For example, given n = 3, a solution set is:
5
+
6
+ [
7
+ "((()))",
8
+ "(()())",
9
+ "(())()",
10
+ "()(())",
11
+ "()()()"
12
+ ]
13
+
14
+ 生成有效的括号对。
15
+
16
+ 关键字:
17
+ 递归
18
+
19
+ beat
20
+ 100%
21
+
22
+ 测试地址:
23
+ https://leetcode.com/problems/generate-parentheses/description/
24
+ """
25
+ class Solution (object ):
26
+ def generateParenthesis (self , n ):
27
+ """
28
+ :type n: int
29
+ :rtype: List[str]
30
+ """
31
+ result = []
32
+
33
+ def _generateParenthesis (x , y , parenthesis ):
34
+ if not x and not y :
35
+ result .append (parenthesis )
36
+ return
37
+
38
+ if y > x :
39
+ _generateParenthesis (x , y - 1 , parenthesis = parenthesis + ')' )
40
+
41
+ if x :
42
+ _generateParenthesis (x - 1 , y , parenthesis = parenthesis + '(' )
43
+
44
+ _generateParenthesis (n - 1 , n , '(' )
45
+
46
+ return result
47
+
You can’t perform that action at this time.
0 commit comments