-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsubset.js
42 lines (37 loc) · 858 Bytes
/
subset.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
40
41
42
// https://www.youtube.com/watch?v=VdnvmfzA1pw
// Given a set of distinct integers, nums, return all possible subsets (the power set).
//
// Note: The solution set must not contain duplicate subsets.
//
// Example:
//
// Input: nums = [1,2,3]
// Output:
// [
// [3],
// [1],
// [2],
// [1,2,3],
// [1,3],
// [2,3],
// [1,2],
// []
// ]
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums, index = 0, current = [], result = [[]]) {
if (current.length === nums.length) {
return;
}
if (index < nums.length) {
current.push(nums[index]);
result.push(current.slice(0));
subsets(nums, index + 1, current, result);
current.pop();
subsets(nums, index + 1, current, result);
}
return result;
};
subsets([1, 2, 3]); //?