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
解题思想: 利用回溯法,不断地往前一个结果的尾部追加左括号或者右括号。
代码:
var generateParenthesis = function(n) { const arr = []; function generate(left, right, str) { // 递归终结条件:n 都用完了 if (left === n && right ===n) { arr.push(str); return; } // 如果左括号没用完(第一肯定先使用左括号),就继续递归 if(left < n) { generate(left + 1, right, str + '('); } // 如果右括号没用完(右括号肯定比左括号少,且有括号没用完),就继续递归 if(right < left) { generate(left, right + 1, str + ')') } } generate(0, 0, ''); return arr; };
复杂度分析:
自己还没实现,后面补 参考sl1673495/leetcode-javascript#31
The text was updated successfully, but these errors were encountered:
No branches or pull requests
方法一:回溯法
解题思想:
利用回溯法,不断地往前一个结果的尾部追加左括号或者右括号。
代码:
复杂度分析:
方法二:动态规划
自己还没实现,后面补
参考sl1673495/leetcode-javascript#31
The text was updated successfully, but these errors were encountered: