【问题标题】:How do I push arrays to array inside a recursive javascript function so that I can use it when it's done?如何将数组推送到递归 javascript 函数中的数组,以便在完成后可以使用它?
【发布时间】:2020-04-06 23:50:12
【问题描述】:

这里的第一个问题(我认为)。请让我知道是否需要其他信息以便你们帮助我。

所以我正在尝试在 javascript 中实现一个使用递归函数的算法。

该函数是从Implementing Heap Algorithm of Permutation in JavaScript复制而来的,如下所示:

let swap = function(array, index1, index2) {
    let temp = array[index1]
    array[index1] = array[index2]
    array[index2] = temp
    return array
}

let permutationHeap = (array, result, n) => {
    n = n || array.length // set n default to array.length
    if (n === 1) {
        result(array)
    } else {
        for (let i = 1; i <= n; i++) {
            permutationHeap(array, result, n - 1)
            if (n % 2) {
                swap(array, 0, n - 1) // when length is odd so n % 2 is 1,  select the first number, then the second number, then the third number. . . to be swapped with the last number
            } else {
                swap(array, i - 1, n - 1) // when length is even so n % 2 is 0,  always select the first number with the last number
            }
        }
    }
}


let output = function(input) {
    console.log(output)
}

permutationHeap([1,2,3,4,5], output)

输出函数(回调?)中的 console.log 给了我正确的输出。如果我将 console.log 移到 permutationHeap-function 中的 if 语句下方,我也会得到正确的输出(虽然在这种情况下是 console.log(array))。

我想要做的是将每个输出存储为一个数组,存储在一个我以后可以使用的数组中。我猜我在这里遇到了 Javascript 101。处理异步思维。但是我一生都无法弄清楚如何获得该数组!

  • 如果我在 permutationHeap 函数之外声明一个空数组并且 .push(array) 它只存储 [1,2,3,4,5]。如果我做同样的事情,同样的交易 输出函数内部的东西。
  • 我还尝试将一个空数组传递给 permutationHeap-function 并以这种方式推送。还是没有运气。

有人愿意为一个可能是超级菜鸟的问题提供一些启发吗? :) 非常感谢!

【问题讨论】:

    标签: javascript arrays recursion


    【解决方案1】:

    我想我已经设法通过使用生成器和产量来修复您的代码。虽然我不能准确地解释它......

    var swap = function(array, index1, index2) {
        let temp = array[index1]
        array[index1] = array[index2]
        array[index2] = temp
        return array
    }
    
    var permutationHeap = function*(array, result, n) {
        n = n || array.length // set n default to array.length
        if (n === 1) {
            yield (array.slice(0))
        } else {
            for (let i = 1; i <= n; i++) {
                yield* permutationHeap(array, result, n - 1)
                if (n % 2) {
                    swap(array, 0, n - 1) // when length is odd so n % 2 is 1,  select the first number, then the second number, then the third number. . . to be swapped with the last number
                } else {
                    swap(array, i - 1, n - 1) // when length is even so n % 2 is 0,  always select the first number with the last number
                }
            }
        }
    }
    
    var x = permutationHeap([1,2,3,4,5])
    var results = Array.from(x);
    console.log(results);

    【讨论】:

    • 感谢您的回复!运行它会给出“未定义输出”。当添加回那个小功能时,console.log(results) 输出一堆 [1,2,3,4,5] 数组:( Somehting I'm missing? 在 node.js 中运行
    • 我编辑了代码,再试一次。这只是我留下的一个未定义的变量,因为我没有使用你的“输出”函数
    • 感谢您的帮助! console.log(results) 输出包含 [1,2,3,4,5] 的数组。所以我猜是缺少了一些东西。
    • 啊,是的,数组需要是函数顶部的副本。在函数顶部添加array = array.slice(0);,我来编辑
    • 没问题,你辛苦了。我只是把yield 放在所有东西上
    【解决方案2】:

    实际上,我在之前的回答中破坏了算法,因为通过重复数组迭代,我破坏了算法中依赖于数组不断变化的部分。

    我已经做出了一个更有效地使用您最初拥有的代码的答案:

    var swap = function(array, index1, index2) {
      var temp = array[index1];
      array[index1] = array[index2];
      array[index2] = temp;
      return array;
    };
    
    var permutationHeap = function(array, result, n) {
      n = n || array.length; // set n default to array.length
      if (n === 1) {
        result(array);
      } else {
        for (var i = 1; i <= n; i++) {
          permutationHeap(array, result, n - 1);
          if (n % 2) {
            swap(array, 0, n - 1); // when length is odd so n % 2 is 1,  select the first number, then the second number, then the third number. . . to be swapped with the last number
          } else {
            swap(array, i - 1, n - 1); // when length is even so n % 2 is 0,  always select the first number with the last number
          }
        }
      }
    };
    
    function getPermutations(array) {
      var results = [];
      var output = function(res) {
        results.push(res.slice(0));
      }
    
      permutationHeap(array, output);
    
      return results;
    }
    
    var permutations = getPermutations([1,2,3]);
    console.log(permutations);

    【讨论】:

    • 我看到我最初接受的答案有一些缺陷。代码看起来很正常,但遗憾的是没有。它现在确实有效!感谢您的所有帮助和意见。
    猜你喜欢
    • 2014-08-31
    • 1970-01-01
    • 2016-06-30
    • 2021-06-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    相关资源
    最近更新 更多