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/problems/permutations-ii/
在文章回溯算法秒杀所有排列/组合/子集问题 - 排列-元素可重不可复选中,提到了一种剪枝思路,javascript代码部分如下图:
如果完全按照示例编写代码会有问题,报错信息如下
输入 nums = [1,1,2] 输出 [[1,2,1]] 预期结果 [[1,1,2],[1,2,1],[2,1,1]]
其中的preNum定义需要放在backTrack函数中,如果放在外层
如上图,会在连续的‘11’时也会判断为需要“剪枝”,预期结果中的[1, 1, 3]则不会被正确push到结果中
The text was updated successfully, but these errors were encountered:
想pr,但是permutations-ii 在 solution_code.md 中并没有找到对应的多语言解法,就直接在下面贴一下javascript的代码(AC)
solution_code.md
/** * @param {number[]} nums * @return {number[][]} */ var permuteUnique = function(nums) { // 输入:nums = [1,1,2] // 输出: // [[1,1,2], // [1,2,1], // [2,1,1]] nums.sort((a, b) => a - b) let used = new Array(nums.length).fill(false) let resultList = [] let temp = [] function backTrack() { if (temp.length === nums.length) { resultList.push([...temp]) return } let preSum = -999 for (let i = 0; i < nums.length; i++) { if (used[i]) { continue } if (nums[i] === preSum) { continue } temp.push(nums[i]) used[i] = true preSum = nums[i] backTrack() temp.pop() used[i] = false } } backTrack() return resultList };
Sorry, something went wrong.
No branches or pull requests
请在提交 bug 之前先搜索
出错的题目链接
https://leetcode.cn/problems/permutations-ii/
报错信息
在文章回溯算法秒杀所有排列/组合/子集问题 - 排列-元素可重不可复选中,提到了一种剪枝思路,javascript代码部分如下图:
如果完全按照示例编写代码会有问题,报错信息如下
其中的preNum定义需要放在backTrack函数中,如果放在外层
如上图,会在连续的‘11’时也会判断为需要“剪枝”,预期结果中的[1, 1, 3]则不会被正确push到结果中
你是否愿意提交 PR 修复这个 bug?
The text was updated successfully, but these errors were encountered: