바위타는 두루미
[leetcode]22. Generate Parentheses 본문
728x90
22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[ "((()))", "(()())", "(())()", "()(())", "()()()" ]
class Solution(object):
def makeCombi(self, prefix, n , openp, closep, result):
#print(prefix)
if openp < closep :
return
if openp+ closep == 2*n :
result.append(prefix)
if openp < n:
self.makeCombi(prefix+"(", n, openp+1, closep, result)
if closep < openp:
self.makeCombi(prefix+")",n, openp, closep+1, result)
return
def generateParenthesis(self, n):
result = []
self.makeCombi("", n , 0, 0, result)
return result
'Study > Algorithm' 카테고리의 다른 글
[leetcode]78. Subsets (0) | 2019.08.10 |
---|---|
[leetcode]46. Permutations (0) | 2019.08.10 |
[leetcode]17. Letter Combinations of a Phone Number (0) | 2019.08.09 |
[leetcode] 200. Number of Islands (0) | 2019.08.09 |
[leetcode]230. Kth Smallest Element in a BST (0) | 2019.08.09 |
Comments