-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubSetsII.js
More file actions
32 lines (30 loc) · 881 Bytes
/
subSetsII.js
File metadata and controls
32 lines (30 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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],
// []
// ]
var subsetsWithDup = function (nums) {
if (!nums) return
if (nums.length === 0) return [[]]
let results = []
nums.sort((a, b) => (a - b))
search(nums, results, [], 0)
return results
}
var search = function (nums, results, subset, startIndex) {
results.push(subset.slice())
for (let i = startIndex; i < nums.length; i++) {
if (i > startIndex && nums[i] == nums[i - 1]) continue; // the only differenct between subSets1 and subSets2.
subset.push(nums[i])
search(nums, results, subset, i + 1) // 递归,startIndex 后移一位
subset.pop()
}
}