-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (35 loc) · 865 Bytes
/
index.js
File metadata and controls
37 lines (35 loc) · 865 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
33
34
35
36
37
/**
* 思路:
*
* 数组中每个数我们可以选择用和不用,缓存一个结果,对结果分别做用很不用的交集
*
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
let result = [];
nums.forEach((num) => {
subsetsHelp(num);
});
return result;
function subsetsHelp(num) {
const new_result = [];
if (result.length === 0) {
result.push([]);
result.push([num]);
} else {
result.forEach((item) => {
new_result.push(item.concat([num]));
new_result.push(item);
});
result = new_result;
}
}
};
console.log(subsets([1,2,3]));
module.exports = {
id:'78',
title:'Subsets',
url:'https://leetcode.com/problems/subsets/',
difficulty:'medium',
};