簡體   English   中英

如何將數組推送到遞歸 javascript 函數內的數組,以便在完成后可以使用它?

[英]How do I push arrays to array inside a recursive javascript function so that I can use it when it's done?

這里的第一個問題(我認為)。 請讓我知道是否需要其他信息以便你們幫助我。

所以我試圖在 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)

輸出函數(回調?)中的 console.log 給了我正確的輸出。 如果我將那個 console.log 移到 permutationHeap-function 中的 if 語句下面,我也會得到正確的輸出(不過在這種情況下是 console.log(array))。

我想要做的是將每個輸出作為一個數組存儲在一個數組中,我以后可以使用它。 我猜我在這里與 Javascript 101 苦苦掙扎。 處理異步思維。 但是我終其一生都無法弄清楚如何獲得該數組!

  • 如果我在 permutationHeap 函數和 .push(array) 之外聲明一個空數組,它只存儲 [1,2,3,4,5]。 如果我在輸出函數中做同樣的事情,同樣的交易。
  • 我還嘗試將一個空數組傳遞給 permutationHeap 函數並以這種方式推送。 仍然沒有運氣。

任何願意為一個可能是超級菜鳥的問題發光的人? :) 非常感謝!

我想我已經通過使用生成器和產量設法修復了您的代碼。 雖然我不能完全解釋它...

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

實際上,我在之前的答案中破壞了算法,因為通過在每次迭代中復制數組,我破壞了依賴於數組隨其變化而變化的算法部分。

我已經做出了一個更有效地使用您最初擁有的代碼的答案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM