标签:
纯递归解法
class Solution: def __init__(self): self.ans = [] def generateParenthesis(self, n): self.help(n, 0, [], "") return self.ans def help(self, n, l, tmp, s): if l == n: if tmp != []: tmp.pop() self.help(n, l, tmp, s + ")") else: self.ans.append(s) else: if tmp == []: tmp.append("(") self.help(n , l + 1, tmp, s + "(") else: self.help(n , l + 1, tmp + ["("], s + "(") tmp.pop() self.help(n, l, tmp, s + ")")
标签:
原文地址:http://www.cnblogs.com/dapanshe/p/4685828.html