题目大意
给定n,生成n对括号,必须正常关闭所有符号
解题思路
深度优先、回溯法典型代表
代码
class Solution(object):
def helpler(self, l, r, item, res):
if r < l:
# print item
return
if l == 0 and r == 0:
res.append(item)
if l > 0:
self.helpler(l - 1, r, item + '(', res)
if r > 0:
self.helpler(l, r - 1, item + ')', res)
def generateParenthesis(self, n):
""" :type n: int :rtype: List[str] """
if n == 0:
return []
res = []
self.helpler(n, n, '', res)
return res