📜  数组置换js递归 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:07.253000             🧑  作者: Mango

代码示例1
const permutations = arr => {
  if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
  return arr.reduce(
    (acc, item, i) =>
      acc.concat(
        permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
          item,
          ...val,
        ])
      ),
    []
  );
};