-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.js
More file actions
47 lines (42 loc) · 1.25 KB
/
permutations.js
File metadata and controls
47 lines (42 loc) · 1.25 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
47
/*
* Given a collection of distinct integers, return all possible permutations.
* Example:
* Input: [1,2,3]
* Output:
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
*/
/*
* Notes: using the example above, first we look at 1 and think of it as a prefix. with that prefix we then have remaining
* numbers 2 and 3. this could either give us (2, 3) or (3, 2).
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
if (!nums || nums.length === 0){ return nums;}
let results = [],
currentArr = [],
currentNum = nums[0];
traverse(nums, currentArr, results);
return results;
};
function traverse(nums, currentArr, results){
// if we've reached the end, then we know that this is a combination that we should add to results
if (nums.length === 0){
results.push(currentArr);
}
// loop through 1, 2, 3
for(num of nums){ // say num is 1
let remainingNums = nums.filter(elem => elem !== num); // remaining numbers would be 2 and 3
// call itself with remainingNums=[2, 3] and a copy of currentArr + num and the results array to augment
traverse(remainingNums, [...currentArr, num], results);
}
}