forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
31 lines (30 loc) · 778 Bytes
/
Copy pathmain.js
File metadata and controls
31 lines (30 loc) · 778 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
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsetsWithDup = function(nums) {
const ans = []
nums.sort((x, y) => x - y)
function subsetHelper (curId, taken, tempArr) {
if (curId === nums.length) {
ans.push(tempArr.slice())
return
}
subsetHelper(curId + 1, false, tempArr)
if (taken || nums[curId] !== nums[curId - 1]) {
/*
[1, 2, (2)]
[1, (2), 2]
(2) means that that 2 is not taken
In this case, these two behavior will yield the exact same result
*/
subsetHelper(curId + 1, true, tempArr.concat([nums[curId]]))
}
}
subsetHelper(1, false, [])
subsetHelper(1, true, [nums[0]])
return ans
};
if (process.env.LZS) {
console.log(subsetsWithDup([2, 1, 1, 2, 2]))
}