-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.py
More file actions
23 lines (20 loc) · 768 Bytes
/
22.py
File metadata and controls
23 lines (20 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
pStack = []
ans = []
if (n > 0):
pStack.append(("(", 1, n - 1, n)) # number represents # of ( left to end
else:
return []
while (len(pStack) > 0):
curEle = pStack.pop()
beginP = curEle[2]
endP = curEle[3]
if (beginP == 0 and endP == 0):
ans.append(curEle[0])
else:
if (beginP > 0):
pStack.append((curEle[0] + "(", curEle[1] + 1, beginP - 1, endP))
if (endP > 0 and curEle[1] > 0):
pStack.append((curEle[0] + ")", curEle[1] - 1, beginP, endP - 1))
return ans