Permutations in JavaScript?

前端 未结 30 2654
不思量自难忘°
不思量自难忘° 2020-11-21 06:52

I\'m trying to write a function that does the following:

  • takes an array of integers as an argument (e.g. [1,2,3,4])
  • creates an array of all the possib
30条回答
  •  爱一瞬间的悲伤
    2020-11-21 07:28

    This is an implementation of Heap's algorithm (similar to @le_m's), except it's recursive.

    function permute_kingzee(arr,n=arr.length,out=[]) {
        if(n == 1) {
            return out.push(arr.slice());
        } else {
            for(let i=0; i

    It looks like it's quite faster too : https://jsfiddle.net/3brqzaLe/

提交回复
热议问题