Appearance
90.子集-ii
题解
js
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsetsWithDup = function (nums) {
const res = [];
let sortNums = nums.sort((a, b) => {
return a - b;
});
function fn(index, path) {
res.push([...path]);
if (index > sortNums.length - 1) {
return;
}
for (let i = index; i < sortNums.length; i++) {
if (i > index && sortNums[i] === sortNums[i - 1]) continue;
fn(i + 1, path.concat(sortNums[i]));
}
}
fn(0, []);
return res;
};
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsetsWithDup = function (nums) {
const res = [];
let sortNums = nums.sort((a, b) => {
return a - b;
});
function fn(index, path) {
res.push([...path]);
if (index > sortNums.length - 1) {
return;
}
for (let i = index; i < sortNums.length; i++) {
if (i > index && sortNums[i] === sortNums[i - 1]) continue;
fn(i + 1, path.concat(sortNums[i]));
}
}
fn(0, []);
return res;
};