Skip to content

Instantly share code, notes, and snippets.

@makotom
Created July 1, 2014 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makotom/e64d10f48112c19f4636 to your computer and use it in GitHub Desktop.
Save makotom/e64d10f48112c19f4636 to your computer and use it in GitHub Desktop.
List permutations
(function () {
"use strict";
var n = 4, k = 4,
ret = [];
{
let pool = [], queue = [];
for (let i = 0; i < n; i += 1) {
pool[i] = i + 1;
queue.push([pool[i]]);
}
while (queue.length > 0) {
let cur = queue.shift();
if (cur.length === k) {
ret.push(cur);
continue;
}
pool.forEach(function (x) {
if (cur.indexOf(x) === - 1) {
let toPush = cur.slice(0);
toPush.push(x);
queue.push(toPush);
}
});
}
}
console.log(ret);
console.log(ret.length);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment