forked from shichao-an/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2.py
More file actions
21 lines (20 loc) · 702 Bytes
/
Copy pathsolution2.py
File metadata and controls
21 lines (20 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
# @param an integer
# @return a list of string
def generateParenthesis(self, n):
cand = []
res = []
self.generate_paren_aux(n, n, cand, res)
return res
def generate_paren_aux(self, left, right, cand, res):
if left == 0 and right == 0:
res.append(''.join(cand))
else:
if left <= right and left > 0:
cand.append('(')
self.generate_paren_aux(left - 1, right, cand, res)
cand.pop()
if left < right and right > 0:
cand.append(')')
self.generate_paren_aux(left, right - 1, cand, res)
cand.pop()