-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.js
More file actions
46 lines (44 loc) · 1.26 KB
/
subsets.js
File metadata and controls
46 lines (44 loc) · 1.26 KB
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
43
44
45
46
/*
* 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) {
let results = [];
let arrayWithPrefix = [],
forPrefix = [];
// say nums = [1,2,3,4]
traverse(nums, 0, results, forPrefix);
return results;
};
// let's walk through this for first loop
function traverse(nums, startingIndex, results, forPrefix){
// take a copy of forPrefix and store in results array
results.push([...forPrefix]); // loop 1: results is [[]]
// loop through [1,2,3,4]
for(let ii=startingIndex;ii<nums.length;ii++){
// let's walkthrough first loop, num=2
forPrefix.push(nums[ii]); // forPrefix is now [1]. this statement is responsible for producing [1], [1,2], [1, 3], [1, 4], then for loop 2 it'll be [2], [2, 3], [2, 4]. each of those will be added to "results" in the next line when it gets to line 17
traverse(nums, ii+1, results, forPrefix);
forPrefix.pop(); // forPrefix is now []
}
}