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
原题链接
本题与 46. 全排列解题思路一样。
不同之处:序列 nums 是可以重复的,要求按任意顺序返回所有不重复的全排列。
used[i]
const permuteUnique = function(nums) { const len = nums.length, res = [], used = [] nums.sort((a, b) => a - b) const backtrack = (deepStack) => { if (deepStack.length === len) { res.push(deepStack.slice()) return } for (let i = 0; i < len; i++) { // 当前选项与上一项相同、且上一项存在、且没有被使用过,则忽略 if (nums[i - 1] === nums[i] && i - 1 >= 0 && !used[i - 1]) continue if (used[i]) continue // 使用过便不再使用 deepStack.push(nums[i]) used[i] = true backtrack(deepStack) deepStack.pop() used[i] = false } } backtrack([]) return res }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接
回溯
本题与 46. 全排列解题思路一样。
不同之处:序列 nums 是可以重复的,要求按任意顺序返回所有不重复的全排列。
used[i]
状态,撤销时也要重置used[i]
状态。The text was updated successfully, but these errors were encountered: