-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem_096.js
More file actions
38 lines (33 loc) Β· 788 Bytes
/
problem_096.js
File metadata and controls
38 lines (33 loc) Β· 788 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
38
/**
* Retrieves all possible permutations
* @param {number[]} list
* @return {number[][]}
*/
function getPermutations(list) {
if (list.length === 0) return [];
const combinations = [];
backtrack(list, combinations, [], new Set());
return combinations;
}
/**
*
* @param {number[]} list
* @param {number[][]} combinations
* @param {number[]} listSoFar
* @param {Set<number>} set
*/
function backtrack(list, combinations, listSoFar, set) {
if (listSoFar.length === list.length) {
combinations.push([...listSoFar]);
return;
}
for (let i = 0; i < list.length; i++) {
const num = list[i];
if (set.has(num)) continue;
listSoFar.push(num);
set.add(num);
backtrack(nums, combinations, listSoFar, set);
listSoFar.pop();
set.delete(num);
}
}