题目描述
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。
思路
代码
from typing import List
class Solution():
def generateParenthesis()->List(str):
res = []
cur_str = ''
def dfs(cur_str,left,right):
if left == 0 and right == 0:
res.append(cur_str)
return
if left > right:
return
if left > 0:
dfs(cur_str + '(',left - 1,right)
if right > 0:
dfs(cur_str + ')',left,right - 1)
dfs(cur_str, n, n)
return res