We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/generate-parentheses/
解题思路:
// 使用递归生成成对括号 function generate(str, left, right, max, result) { // 递归终结条件 // 当生成的字符串长度等于最大长度时结束循环 if (str.length === max * 2) { // 由于字符串生成时已经进行了括号成对的判断,此处只需要存储结果 result.push(str); // 结束递归 return; } // 当前层的递归逻辑以及继续递归。 // 当左括号小于对数时,表示可以继续生成一对括号 // 此时先增加一个左括号。右括号可以再下一次递归补齐。 // 同时左括号数量加1 if (left < max) { generate(str + '(', left + 1, right, max, result); } // 当左括号数量大于右括号时,表示可以补充右括号组成一对括号 // 同时右括号数量加1 if (left > right) { generate(str + ')', left, right + 1, max, result); } } /** * @param {number} n * @return {string[]} */ var generateParenthesis = function (n) { let result = []; // 储存结果 // 使用递归生成成对括号 generate( '', // 初始字符串为空 0, // 左括号计数 0, // 右括号计数 n, // 括号对数 result, // 储存结果 ); return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/generate-parentheses/
解题思路:
The text was updated successfully, but these errors were encountered: