js 输入一个数组 输出该数组的所有排列组合

Input: [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
function calculate (nums) {
  let result = [];
  let go = function (currentArr) {
    if(currentArr.length === nums.length){
      result.push(currentArr);
      return;
    }
    nums.forEach(v => {
      if(!currentArr.includes(v)){
        go([...currentArr, v]);
      }
    })
  }
  go([])
  return result
}

 

posted @ 2022-02-24 11:28  Mr_R  阅读(948)  评论(0编辑  收藏  举报