要求:
输入:n = 3
输出:[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]
function aa(n) {
let list = []; // 创建存储的空数组
let str = ''; // 创建初始值
return function deep(left, right, str) {
if (left === 0 && right === 0) { //当左箭头和右箭头全部用完时 return
list.push(str) // 将生成括号添加到 list 中
return
}
if (left > 0) { //当还有左箭头时
deep(left - 1, right, str + '(') // 添加左箭头,left -1
}
if (left < right) { // 当 还剩下右箭头的数量大于 左箭头时
deep(left, right - 1, str + ')') // 添加右箭头
}
return list
}(n, n, str)
}
const a = aa(3);
console.log(a);