forked from lanqiao-courses/python-100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path063-pair.py
22 lines (20 loc) · 800 Bytes
/
063-pair.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Parentheses(object):
def find_pair(self, num_pairs):
if num_pairs is None:
raise TypeError('num_pairs cannot be None')
if num_pairs < 0:
raise ValueError('num_pairs cannot be < 0')
if not num_pairs:
return []
results = []
curr_results = []
self._find_pair(num_pairs, num_pairs, curr_results, results)
return results
def _find_pair(self, nleft, nright, curr_results, results):
if nleft == 0 and nright == 0:
results.append(''.join(curr_results))
else:
if nleft >= 0:
self._find_pair(nleft-1, nright, curr_results+['('], results)
if nright > nleft:
self._find_pair(nleft, nright-1, curr_results+[')'], results)