-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (72 loc) · 1.95 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function permute(arr) {
var N = arr.length;
var directions = [];
var indices = [];
directions.push(0);
indices.push(0);
for (var i = 1; i < N; i += 1) {
directions.push(-1);
indices.push(i);
}
function swap(i, j) {
var tmp = indices[i];
indices[i] = indices[j];
indices[j] = tmp;
tmp = directions[i];
directions[i] = directions[j];
directions[j] = tmp;
}
function result() {
var res = [];
for (var i = 0; i < N; i += 1) {
res.push(arr[indices[i]]);
}
return res;
}
var makeResult = typeof arr !== 'string' ? result : function () {
return result().join('');
};
return function () {
var i, maxIndex, moveTo;
for (i = 0; i < N; i += 1) {
if (directions[i] !== 0) {
maxIndex = i;
break;
}
}
if (maxIndex === undefined) {
return undefined;
}
for (i = maxIndex + 1; i < N; i += 1) {
if (directions[i] !== 0 && indices[i] > indices[maxIndex]) {
maxIndex = i;
}
}
moveTo = maxIndex + directions[maxIndex];
swap(maxIndex, moveTo);
if (moveTo === 0 || moveTo === N - 1 || indices[moveTo + directions[moveTo]] > indices[moveTo]) {
directions[moveTo] = 0;
}
for (i = 0; i < N; i += 1) {
if (indices[i] > indices[moveTo]) {
if (i < moveTo) {
directions[i] = 1;
} else {
directions[i] = -1;
}
}
}
return makeResult();
};
};
permute.all = function (arr) {
var generator = permute(arr);
var next = arr;
var result = [];
while (next !== undefined) {
result.push(next);
next = generator();
}
return result;
};
module.exports = permute;