JavaScript中的排列?

JavaScript中的排列?

我正在编写一个函数,它执行以下操作:

  • 将整数数组作为参数(例如[1,2,3,4])
  • 创建所有可能的[1,2,3,4]排列的数组,每个置换的长度为4

下面的函数(我在网上找到的)通过接受一个字符串作为参数,并返回该字符串的所有排列来实现这一点。

我想不出如何修改它,使其与整数数组一起工作(我认为这与某些方法在字符串上的工作方式与它们在整数上的工作方式不同有关,但我不确定.)

var permArr = [], usedChars = [];function permute(input) {
  var i, ch, chars = input.split("");
  for (i = 0; i < chars.length; i++) {
    ch = chars.splice(i, 1);
    usedChars.push(ch);
    if (chars.length == 0)
      permArr[permArr.length] = usedChars.join("");
    permute(chars.join(""));
    chars.splice(i, 0, ch);
    usedChars.pop();
  }
  return permArr};

注意:我希望使函数返回数组整数一列.

我真的需要使用JavaScript的解决方案。我已经想出了如何在python中做到这一点。


桃花长相依
浏览 490回答 2
2回答

陪伴而非守候

如果您注意到,代码实际上在进行任何排列之前将字符拆分成一个数组,因此您只需删除联接和拆分操作即可。var permArr = [],&nbsp; usedChars = [];function permute(input) {&nbsp; var i, ch;&nbsp; for (i = 0; i < input.length; i++) {&nbsp; &nbsp; ch = input.splice(i, 1)[0];&nbsp; &nbsp; usedChars.push(ch);&nbsp; &nbsp; if (input.length == 0) {&nbsp; &nbsp; &nbsp; permArr.push(usedChars.slice());&nbsp; &nbsp; }&nbsp; &nbsp; permute(input);&nbsp; &nbsp; input.splice(i, 0, ch);&nbsp; &nbsp; usedChars.pop();&nbsp; }&nbsp; return permArr};document.write(JSON.stringify(permute([5, 3, 7, 1])));

当年话下

以下非常有效的算法使用堆法要生成在O(N!)中具有运行时复杂性的N个元素的所有排列:function permute(permutation) {&nbsp; var length = permutation.length,&nbsp; &nbsp; &nbsp; result = [permutation.slice()],&nbsp; &nbsp; &nbsp; c = new Array(length).fill(0),&nbsp; &nbsp; &nbsp; i = 1, k, p;&nbsp; while (i < length) {&nbsp; &nbsp; if (c[i] < i) {&nbsp; &nbsp; &nbsp; k = i % 2 && c[i];&nbsp; &nbsp; &nbsp; p = permutation[i];&nbsp; &nbsp; &nbsp; permutation[i] = permutation[k];&nbsp; &nbsp; &nbsp; permutation[k] = p;&nbsp; &nbsp; &nbsp; ++c[i];&nbsp; &nbsp; &nbsp; i = 1;&nbsp; &nbsp; &nbsp; result.push(permutation.slice());&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; c[i] = 0;&nbsp; &nbsp; &nbsp; ++i;&nbsp; &nbsp; }&nbsp; }&nbsp; return result;}console.log(permute([1, 2, 3]));相同的算法实现为发电机空间复杂度为O(N):function* permute(permutation) {&nbsp; var length = permutation.length,&nbsp; &nbsp; &nbsp; c = Array(length).fill(0),&nbsp; &nbsp; &nbsp; i = 1, k, p;&nbsp; yield permutation.slice();&nbsp; while (i < length) {&nbsp; &nbsp; if (c[i] < i) {&nbsp; &nbsp; &nbsp; k = i % 2 && c[i];&nbsp; &nbsp; &nbsp; p = permutation[i];&nbsp; &nbsp; &nbsp; permutation[i] = permutation[k];&nbsp; &nbsp; &nbsp; permutation[k] = p;&nbsp; &nbsp; &nbsp; ++c[i];&nbsp; &nbsp; &nbsp; i = 1;&nbsp; &nbsp; &nbsp; yield permutation.slice();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; c[i] = 0;&nbsp; &nbsp; &nbsp; ++i;&nbsp; &nbsp; }&nbsp; }}// Memory efficient iteration through permutations:for (var permutation of permute([1, 2, 3])) console.log(permutation);// Simple array conversion:var permutations = [...permute([1, 2, 3])];请随意将您的实现添加到以下内容基准js测试套件:function permute_SiGanteng(input) {&nbsp; var permArr = [],&nbsp; &nbsp; usedChars = [];&nbsp; function permute(input) {&nbsp; &nbsp; var i, ch;&nbsp; &nbsp; for (i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; ch = input.splice(i, 1)[0];&nbsp; &nbsp; &nbsp; usedChars.push(ch);&nbsp; &nbsp; &nbsp; if (input.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; permArr.push(usedChars.slice());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; permute(input);&nbsp; &nbsp; &nbsp; input.splice(i, 0, ch);&nbsp; &nbsp; &nbsp; usedChars.pop();&nbsp; &nbsp; }&nbsp; &nbsp; return permArr&nbsp; }&nbsp; return permute(input);}function permute_delimited(inputArr) {&nbsp; var results = [];&nbsp; function permute(arr, memo) {&nbsp; &nbsp; var cur, memo = memo || [];&nbsp; &nbsp; for (var i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; cur = arr.splice(i, 1);&nbsp; &nbsp; &nbsp; if (arr.length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; results.push(memo.concat(cur));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; permute(arr.slice(), memo.concat(cur));&nbsp; &nbsp; &nbsp; arr.splice(i, 0, cur[0]);&nbsp; &nbsp; }&nbsp; &nbsp; return results;&nbsp; }&nbsp; return permute(inputArr);}function permute_monkey(inputArray) {&nbsp; return inputArray.reduce(function permute(res, item, key, arr) {&nbsp; &nbsp; return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(function(perm) {&nbsp; &nbsp; &nbsp; return [item].concat(perm);&nbsp; &nbsp; }) || item);&nbsp; }, []);}function permute_Oriol(input) {&nbsp; var permArr = [],&nbsp; &nbsp; usedChars = [];&nbsp; return (function main() {&nbsp; &nbsp; for (var i = 0; i < input.length; i++) {&nbsp; &nbsp; &nbsp; var ch = input.splice(i, 1)[0];&nbsp; &nbsp; &nbsp; usedChars.push(ch);&nbsp; &nbsp; &nbsp; if (input.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; permArr.push(usedChars.slice());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; main();&nbsp; &nbsp; &nbsp; input.splice(i, 0, ch);&nbsp; &nbsp; &nbsp; usedChars.pop();&nbsp; &nbsp; }&nbsp; &nbsp; return permArr;&nbsp; })();}function permute_MarkusT(input) {&nbsp; function permutate(array, callback) {&nbsp; &nbsp; &nbsp; function p(array, index, callback) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function swap(a, i1, i2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var t = a[i1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i1] = a[i2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i2] = t;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index == array.length - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(array);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var count = p(array, index + 1, callback);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = index + 1; i < array.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(array, i, index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += p(array, index + 1, callback);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(array, i, index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (!array || array.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return p(array, 0, callback);&nbsp; }&nbsp; var result = [];&nbsp; permutate(input, function(a) {&nbsp; &nbsp; &nbsp; result.push(a.slice(0));&nbsp; });&nbsp; return result;}function permute_le_m(permutation) {&nbsp; var length = permutation.length,&nbsp; result = [permutation.slice()],&nbsp; &nbsp; &nbsp; c = new Array(length).fill(0),&nbsp; &nbsp; &nbsp; i = 1, k, p;&nbsp;&nbsp;&nbsp; while (i < length) {&nbsp; &nbsp; if (c[i] < i) {&nbsp; &nbsp; &nbsp; k = i % 2 && c[i],&nbsp; &nbsp; &nbsp; p = permutation[i];&nbsp; &nbsp; &nbsp; permutation[i] = permutation[k];&nbsp; &nbsp; &nbsp; permutation[k] = p;&nbsp; &nbsp; &nbsp; ++c[i];&nbsp; &nbsp; &nbsp; i = 1;&nbsp; &nbsp; &nbsp; result.push(permutation.slice());&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; c[i] = 0;&nbsp; &nbsp; &nbsp; ++i;&nbsp; &nbsp; }&nbsp; }&nbsp; return result;}function permute_Urielzen(arr) {&nbsp; &nbsp; var finalArr = [];&nbsp; &nbsp; var iterator = function (arrayTaken, tree) {&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < tree; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var temp = arrayTaken.slice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp.splice(tree - 1 - i, 0, temp.splice(tree - 1, 1)[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tree >= arr.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalArr.push(temp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { iterator(temp, tree + 1); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; iterator(arr, 1); return finalArr;}function permute_Taylor_Hakes(arr) {&nbsp; var permutations = [];&nbsp; if (arr.length === 1) {&nbsp; &nbsp; return [ arr ];&nbsp; }&nbsp; for (var i = 0; i <&nbsp; arr.length; i++) {&nbsp;&nbsp; &nbsp; var subPerms = permute_Taylor_Hakes(arr.slice(0, i).concat(arr.slice(i + 1)));&nbsp; &nbsp; for (var j = 0; j < subPerms.length; j++) {&nbsp; &nbsp; &nbsp; subPerms[j].unshift(arr[i]);&nbsp; &nbsp; &nbsp; permutations.push(subPerms[j]);&nbsp; &nbsp; }&nbsp; }&nbsp; return permutations;}var Combinatorics = (function () {&nbsp; &nbsp; 'use strict';&nbsp; &nbsp; var version = "0.5.2";&nbsp; &nbsp; /* combinatory arithmetics */&nbsp; &nbsp; var P = function(m, n) {&nbsp; &nbsp; &nbsp; &nbsp; var p = 1;&nbsp; &nbsp; &nbsp; &nbsp; while (n--) p *= m--;&nbsp; &nbsp; &nbsp; &nbsp; return p;&nbsp; &nbsp; };&nbsp; &nbsp; var C = function(m, n) {&nbsp; &nbsp; &nbsp; &nbsp; if (n > m) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return P(m, n) / P(n, n);&nbsp; &nbsp; };&nbsp; &nbsp; var factorial = function(n) {&nbsp; &nbsp; &nbsp; &nbsp; return P(n, n);&nbsp; &nbsp; };&nbsp; &nbsp; var factoradic = function(n, d) {&nbsp; &nbsp; &nbsp; &nbsp; var f = 1;&nbsp; &nbsp; &nbsp; &nbsp; if (!d) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (d = 1; f < n; f *= ++d);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f > n) f /= d--;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f = factorial(d);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var result = [0];&nbsp; &nbsp; &nbsp; &nbsp; for (; d; f /= d--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[d] = Math.floor(n / f);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n %= f;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; };&nbsp; &nbsp; /* common methods */&nbsp; &nbsp; var addProperties = function(dst, src) {&nbsp; &nbsp; &nbsp; &nbsp; Object.keys(src).forEach(function(p) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(dst, p, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: src[p],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; configurable: p == 'next'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };&nbsp; &nbsp; var hideProperty = function(o, p) {&nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(o, p, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable: true&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; };&nbsp; &nbsp; var toArray = function(f) {&nbsp; &nbsp; &nbsp; &nbsp; var e, result = [];&nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; while (e = this.next()) result.push(f ? f(e) : e);&nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; };&nbsp; &nbsp; var common = {&nbsp; &nbsp; &nbsp; &nbsp; toArray: toArray,&nbsp; &nbsp; &nbsp; &nbsp; map: toArray,&nbsp; &nbsp; &nbsp; &nbsp; forEach: function(f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (e = this.next()) f(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; filter: function(f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var e, result = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (e = this.next()) if (f(e)) result.push(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.init();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; lazyMap: function(f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._lazyMap = f;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; lazyFilter: function(f) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(this, 'next', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof f !== 'function') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.next = this._next;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeof (this._next) !== 'function') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._next = this.next;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var _next = this._next.bind(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.next = (function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (e = _next()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f(e))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return e;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).bind(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object.defineProperty(this, 'next', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writable: false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; /* power set */&nbsp; &nbsp; var power = function(ary, fun) {&nbsp; &nbsp; &nbsp; &nbsp; var size = 1 << ary.length,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sizeOf = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; that = Object.create(ary.slice(), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get: sizeOf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; hideProperty(that, 'index');&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueOf: sizeOf,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; that.index = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nth: function(n) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n >= size) return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (; n; n >>>= 1, i++) if (n & 1) result.push(this[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (typeof (that._lazyMap) === 'function')?that._lazyMap(result):result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.nth(this.index++);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, common);&nbsp; &nbsp; &nbsp; &nbsp; that.init();&nbsp; &nbsp; &nbsp; &nbsp; return (typeof (fun) === 'function') ? that.map(fun) : that;&nbsp; &nbsp; };&nbsp; &nbsp; /* combination */&nbsp; &nbsp; var nextIndex = function(n) {&nbsp; &nbsp; &nbsp; &nbsp; var smallest = n & -n,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ripple = n + smallest,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_smallest = ripple & -ripple,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ones = ((new_smallest / smallest) >> 1) - 1;&nbsp; &nbsp; &nbsp; &nbsp; return ripple | ones;&nbsp; &nbsp; };&nbsp; &nbsp; var combination = function(ary, nelem, fun) {&nbsp; &nbsp; &nbsp; &nbsp; if (!nelem) nelem = ary.length;&nbsp; &nbsp; &nbsp; &nbsp; if (nelem < 1) throw new RangeError;&nbsp; &nbsp; &nbsp; &nbsp; if (nelem > ary.length) throw new RangeError;&nbsp; &nbsp; &nbsp; &nbsp; var first = (1 << nelem) - 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = C(ary.length, nelem),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxIndex = 1 << ary.length,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sizeOf = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; that = Object.create(ary.slice(), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get: sizeOf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; hideProperty(that, 'index');&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueOf: sizeOf,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.index = first;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.index >= maxIndex) return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = this.index,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (; n; n >>>= 1, i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n & 1) result[result.length] = this[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.index = nextIndex(this.index);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (typeof (that._lazyMap) === 'function')?that._lazyMap(result):result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, common);&nbsp; &nbsp; &nbsp; &nbsp; that.init();&nbsp; &nbsp; &nbsp; &nbsp; return (typeof (fun) === 'function') ? that.map(fun) : that;&nbsp; &nbsp; };&nbsp; &nbsp; /* permutation */&nbsp; &nbsp; var _permutation = function(ary) {&nbsp; &nbsp; &nbsp; &nbsp; var that = ary.slice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; size = factorial(that.length);&nbsp; &nbsp; &nbsp; &nbsp; that.index = 0;&nbsp; &nbsp; &nbsp; &nbsp; that.next = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.index >= size) return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var copy = this.slice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digits = factoradic(this.index, this.length),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = [],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = this.length - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (; i >= 0; --i) result.push(copy.splice(digits[i], 1)[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.index++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (typeof (that._lazyMap) === 'function')?that._lazyMap(result):result;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; return that;&nbsp; &nbsp; };&nbsp; &nbsp; // which is really a permutation of combination&nbsp; &nbsp; var permutation = function(ary, nelem, fun) {&nbsp; &nbsp; &nbsp; &nbsp; if (!nelem) nelem = ary.length;&nbsp; &nbsp; &nbsp; &nbsp; if (nelem < 1) throw new RangeError;&nbsp; &nbsp; &nbsp; &nbsp; if (nelem > ary.length) throw new RangeError;&nbsp; &nbsp; &nbsp; &nbsp; var size = P(ary.length, nelem),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sizeOf = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; that = Object.create(ary.slice(), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get: sizeOf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; hideProperty(that, 'cmb');&nbsp; &nbsp; &nbsp; &nbsp; hideProperty(that, 'per');&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valueOf: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return size;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.cmb = combination(ary, nelem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.per = _permutation(this.cmb.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = this.per.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cmb = this.cmb.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!cmb) return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.per = _permutation(cmb);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return this.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (typeof (that._lazyMap) === 'function')?that._lazyMap(result):result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; addProperties(that, common);&nbsp; &nbsp; &nbsp; &nbsp; that.init();&nbsp; &nbsp; &nbsp; &nbsp; return (typeof (fun) === 'function') ? that.map(fun) : that;&nbsp; &nbsp; };&nbsp; &nbsp; /* export */&nbsp; &nbsp; var Combinatorics = Object.create(null);&nbsp; &nbsp; addProperties(Combinatorics, {&nbsp; &nbsp; &nbsp; &nbsp; C: C,&nbsp; &nbsp; &nbsp; &nbsp; P: P,&nbsp; &nbsp; &nbsp; &nbsp; factorial: factorial,&nbsp; &nbsp; &nbsp; &nbsp; factoradic: factoradic,&nbsp; &nbsp; &nbsp; &nbsp; permutation: permutation,&nbsp; &nbsp; });&nbsp; &nbsp; return Combinatorics;})();var suite = new Benchmark.Suite;var input = [0, 1, 2, 3, 4];suite.add('permute_SiGanteng', function() {&nbsp; &nbsp; permute_SiGanteng(input);&nbsp; })&nbsp; .add('permute_delimited', function() {&nbsp; &nbsp; permute_delimited(input);&nbsp; })&nbsp; .add('permute_monkey', function() {&nbsp; &nbsp; permute_monkey(input);&nbsp; })&nbsp; .add('permute_Oriol', function() {&nbsp; &nbsp; permute_Oriol(input);&nbsp; })&nbsp; .add('permute_MarkusT', function() {&nbsp; &nbsp; permute_MarkusT(input);&nbsp; })&nbsp; .add('permute_le_m', function() {&nbsp; &nbsp; permute_le_m(input);&nbsp; })&nbsp; .add('permute_Urielzen', function() {&nbsp; &nbsp; permute_Urielzen(input);&nbsp; })&nbsp; .add('permute_Taylor_Hakes', function() {&nbsp; &nbsp; permute_Taylor_Hakes(input);&nbsp; })&nbsp; .add('permute_Combinatorics', function() {&nbsp; &nbsp; Combinatorics.permutation(input).toArray();&nbsp; })&nbsp; .on('cycle', function(event) {&nbsp; &nbsp; console.log(String(event.target));&nbsp; })&nbsp; .on('complete', function() {&nbsp; &nbsp; console.log('Fastest is ' + this.filter('fastest').map('name'));&nbsp; })&nbsp; .run({async: true});<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.4/platform.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script>Chrome 48的运行时结果:99.152毫秒这种算法153.115毫秒马库斯401.255毫秒JS-组合学497.037ms乌列尔岑925.669ms猎户座1026.571毫秒西甘蒂斯2529.841毫秒定界3992.622ms猴子4514.665ms猎户座
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript