mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 03:27:07 +00:00
Time: 21 ms (89.04%), Space: 13.7 MB (67.89%) - LeetHub
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
class Solution(object):
|
||||
def generateParenthesis(self, n):
|
||||
"""
|
||||
:type n: int
|
||||
:rtype: List[str]
|
||||
"""
|
||||
return self.createSets("(", 1, n - 1)
|
||||
|
||||
def createSets(self, current, opened, yetToOpen):
|
||||
if opened == 0 and yetToOpen == 0:
|
||||
return [current]
|
||||
|
||||
returnSet = []
|
||||
if yetToOpen > 0:
|
||||
returnSet += self.createSets(current + "(", opened + 1, yetToOpen - 1)
|
||||
|
||||
if opened > 0:
|
||||
returnSet += self.createSets(current + ")", opened - 1, yetToOpen)
|
||||
|
||||
return returnSet
|
||||
Reference in New Issue
Block a user