-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path90.子集.js
39 lines (36 loc) · 1.04 KB
/
90.子集.js
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
33
34
35
36
37
38
39
var subsetsWithDup = function (nums) {
nums.sort((a, b) => a - b);
let result = [];
for (let index = 1; index < nums.length + 1; index++) {
let temp = unit(nums, index);
result.push(...temp);
}
result.push([]);
return [
...new Set(
result.map((e) => {
return JSON.stringify(e);
})
),
].map(JSON.parse);
function unit(arr, n) {
let functionText = `let result=[];`;
loopTextSuffix = ``;
for (let count = 1; count <= n; count++) {
let placeholder = `index${count}`;
functionText += `for(let ${placeholder}=${
count > 1 ? `index${count - 1}` : -1
}+1;${placeholder}<${arr.length};${placeholder}++){`;
loopTextSuffix += `}\n`;
}
functionText += `result.push([${new Array(n)
.fill(undefined)
.map((e, index) => {
return `arr[index${index + 1}]`;
})}])`;
functionText += loopTextSuffix;
functionText += "return result;";
let functionEntity = new Function("arr", "n", functionText);
return functionEntity(arr, n);
}
};