题解 | #括号生成#
括号生成
https://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return string字符串一维数组
#
class Solution:
def generateParenthesis(self , n: int) -> List[str]:
# write code here
def recursion(left, right, tmp, res, n):
if left == n and right == n:
res.append(tmp)
return
if left < n:
recursion(left+1, right, tmp+'(', res, n)
if right < n and left > right:
recursion(left, right+1, tmp+')', res, n)
res = []
tmp = ""
recursion(0, 0, tmp, res, n)
return res

