如何将数组推送到递归 JavaScript 函数中的数组,以便在完成后可以使用它?



第一个问题在这里(我想(。如果需要其他信息,请告诉我,以便你们帮助我。

所以我正在尝试在javascript中实现一种使用递归功能的算法。

该函数是从 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)

控制台.log在输出函数(回调?(中为我提供了正确的输出。如果我将该控制台移动到排列堆函数中的 if 语句下方.log我也会得到正确的输出(在这种情况下,console.log(array(。

我想做的是将每个输出存储为一个数组,存储在一个数组中,以便以后使用。我猜我在这里正在努力使用Javascript 101。处理异步思维。但是我一辈子都想不出如何得到那个数组数组!

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

有谁愿意在一个可能超级菜鸟的问题上发光吗? :)非常感谢!

我实际上在之前的答案中破坏了算法,因为通过每次迭代复制数组,我破坏了算法中依赖于数组随之变化的部分。

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

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);

我想我已经设法通过使用生成器和产量来修复您的代码。不过我不能完全解释...

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);

最新更新