题解 | #括号生成#
括号生成
https://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca
class Solution:
def generateParenthesis(self , n: int) -> List[str]:
# write code here
s = '('*n + ')'*n
rst = {}
def dfs(l, r, s):
if l == n and r == n:
if s not in rst:
rst[s] = 1
return
if l <= n:
dfs(l + 1, r, s + '(')
if r <= n and l > r:
dfs(l, r+1, s + ')')
dfs(0, 0, '')
return list(rst.keys())
搞清楚dfs的方向
