给你一个整数数组 nums,其中可能包含重复元素,请你返回该数组所有可能的子集。解集不能包含重复的子集。
解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var subsetsWithDup = function (nums) { nums.sort((a, b) => a - b); const result = []; function backtrack(start, path) { result.push([...path]); for (let i = start; i < nums.length; i++) { if (i > start && nums[i] === nums[i - 1]) continue; path.push(nums[i]); backtrack(i + 1, path); path.pop(); } } backtrack(0, []); return result; };
|