-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
26 lines (25 loc) · 749 Bytes
/
test.js
File metadata and controls
26 lines (25 loc) · 749 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
function test(arr, unOrderIndex) {
const unOrderArr = [];
for (let i = 0; i < unOrderIndex.length; i++) {
unOrderArr.push(arr[unOrderIndex[i]]);
arr[unOrderIndex[i]] = undefined;
}
const res = [];
const help = (start) => {
if (arr.every((item) => item !== undefined)) {
res.push([...arr]);
return;
}
const first = unOrderIndex[start];
for (let i = 0; i < unOrderIndex.length; i++) {
if (arr.indexOf(unOrderArr[i]) > -1) continue;
arr[first] = unOrderArr[i];
help(start + 1);
arr[first] = undefined;
}
};
help(0);
return res;
}
const res = test([0, 5, 1, 2], [0, 1, 3]);
console.log(res);