Created
February 22, 2019 18:06
-
-
Save monsterooo/9b3d205b53cb995e2d444e2a2d8e3d8b to your computer and use it in GitHub Desktop.
生成括号问题-卡特兰数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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