-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathpermutateWithoutRepetitions.js
More file actions
31 lines (25 loc) · 1.27 KB
/
Copy pathpermutateWithoutRepetitions.js
File metadata and controls
31 lines (25 loc) · 1.27 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
/**
* @param {*[]} permutationOptions
* @return {*[]}
*/
export default function permutateWithoutRepetitions(permutationOptions) {
if (permutationOptions.length === 1) {
return [permutationOptions];
}
// Khởi tạo mảng hoán vị.
const permutations = [];
// Lấy tất cả hoán vị cho permutationOptions ngoại trừ phần tử đầu tiên.
const smallerPermutations = permutateWithoutRepetitions(permutationOptions.slice(1));
// Thêm lựa chọn đầu tiên vào mọi vị trí có thể có của mọi hoán vị nhỏ hơn.
const firstOption = permutationOptions[0];
for (let permIndex = 0; permIndex < smallerPermutations.length; permIndex += 1) {
const smallerPermutation = smallerPermutations[permIndex];
// Thêm lựa chọn đầu tiên vào mọi vị trí có thể có của smallerPermutation.
for (let positionIndex = 0; positionIndex <= smallerPermutation.length; positionIndex += 1) {
const permutationPrefix = smallerPermutation.slice(0, positionIndex);
const permutationSuffix = smallerPermutation.slice(positionIndex);
permutations.push(permutationPrefix.concat([firstOption], permutationSuffix));
}
}
return permutations;
}