Subsets II
Description
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
Solution(javascript)
// 关键是如何去重或者避免重复
const subsetsWithDup = (nums = []) => {
const result = []
nums.sort((a, b) => a - b)
const aux = (index, current) => {
result.push(current)
for (let i = index; i < nums.length; i++) {
if (i > index && nums[i] === nums[i - 1]) {
continue
}
aux(i + 1, [...current, nums[i]])
}
}
aux(0, [])
return result
}