Generate permutations of JavaScript array

后端 未结 4 1405
失恋的感觉
失恋的感觉 2020-11-27 06:20

I have an array of n different elements in javascript, I know there are n! possible ways to order these elements. I want to know what\'s the most effective (fastest) algorit

4条回答
  •  执笔经年
    2020-11-27 06:47

    It is just for fun - my recursive solve in one string

    const perm = a => a.length ? a.reduce((r, v, i) => [ ...r, ...perm([ ...a.slice(0, i), ...a.slice(i + 1) ]).map(x => [ v, ...x ])], []) : [[]]
    

提交回复
热议问题