-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46Permutations.js
More file actions
52 lines (46 loc) · 1.37 KB
/
Copy path46Permutations.js
File metadata and controls
52 lines (46 loc) · 1.37 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
48
49
50
51
var permute = function(nums){
const output = [];
function dfs(curr, rest) {
if(rest.length == 0){
output.push(curr);
return;
}
for(let i = 0; i < rest.length; i++){
dfs([...curr, rest[i]], [...rest.slice(0,i), ...rest.slice(i+1)]);
}
}
dfs([],nums);
return output;
}
/**
* The goal is break down the problem by finding permutations in subarrays.
* So we will maintain a subarray of fixed elements and a subarray for
* exploring permutations.
*
* [1], [2, 3] [1, 2], [3] [1, 2, 3]
* [], [1, 2, 3] -> [2], [1, 3] -> [1, 3], [2] -> [1, 3, 2]
* [3], [1, 2] [2, 1], [3] [2, 1, 3]
* [2, 3], [1] [2, 3, 1]
* [3, 1], [2] [3, 1, 2]
* [3, 2], [1] [3, 2, 1]
*/
/******Sol 2: Whether it is used or not**** */
var permute = function(nums) {
let res = [];
dfs(nums, [], Array(nums.length).fill(false),res);
return res;
}
function dfs(letters, path, used, res) {
if(path.length == letters.length) {
res.push(Array.from(path));
return;
}
for(let i = 0; i<letters.length;i++){
if(used[i]) continue;
path.push(letters[i]);
used[i] = true;
dfs(letters, path, used, res);
path.pop();
used[i] = false;
}
}