Skip to content

Instantly share code, notes, and snippets.

@monsterooo
Created February 22, 2019 18:06
Show Gist options
  • Save monsterooo/9b3d205b53cb995e2d444e2a2d8e3d8b to your computer and use it in GitHub Desktop.
Save monsterooo/9b3d205b53cb995e2d444e2a2d8e3d8b to your computer and use it in GitHub Desktop.
生成括号问题-卡特兰数
function generateBracket(res, str, left, right) {
if (left > right) return;
console.log(`str > ${str}, left > ${left}, right > ${right}`)
if (left > 0) {
generateBracket(res, str + '(', left - 1, right);
}
if (right > 0) {
generateBracket(res, str + ')', left, right - 1);
}
if (left === 0 && right === 0) {
res.push(str)
return;
}
}
var res = []
generateBracket(res, '', 3, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment