-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22_generate_parentheses.py
More file actions
39 lines (29 loc) · 1.06 KB
/
22_generate_parentheses.py
File metadata and controls
39 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from typing import List
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
def generate_parentheses(left, right, string):
if left == right == 0:
solutions.append(string)
return
if left:
generate_parentheses(left - 1, right, string + "(")
if right > left:
generate_parentheses(left, right - 1, string + ")")
solutions = []
generate_parentheses(n, n, "")
return solutions
def generateParenthesis2(self, n: int) -> List[str]:
solutions = []
if n == 0:
return solutions
queue = [("", n, n)] # val, left, right
while queue:
node = queue.pop(0)
if node[2] == 0:
solutions.append(node[0])
continue
if node[1] > 0:
queue.append((node[0] + "(", node[1] - 1, node[2]))
if node[2] > node[1]:
queue.append((node[0] + ")", node[1], node[2] - 1))
return solutions