简体   繁体   English

Array.push正在推送一个空数组,而不是我传递给它的完整数组

[英]Array.push is pushing an empty array instead of the full array that I am passing to it

I'm writing a solution for the Climbing Staircase Algorithm. 我正在为“爬楼梯算法”编写解决方案。
The prompt is specifically: 提示特别是:

'You need to climb a staircase that has n steps, and you decide to get some extra exercise by jumping up the steps. “您需要爬上n个台阶的楼梯,然后决定跳上台阶进行一些额外的锻炼。 You can cover at most k steps in a single jump. 一次跳转最多可以覆盖k个步骤。 Return all the possible sequences of jumps that you could take to climb the staircase, sorted.' 返回您可能要爬上楼梯的所有可能跳序列,按顺序进行。

My code works - I utilized backtracking, and it should all come out in the correct order EXCEPT... whenever I push a solution into my answer array, an empty array is getting pushed instead. 我的代码有效-我利用了回溯,并且所有这些都应该以正确的顺序出现,除了...每当我将解决方案推入答案数组时,就会推入一个空数组。 I console logged to see what was getting pushed into my array, and it's a full array with the correct values. 我控制台登录以查看将什么推送到我的数组中,并且它是具有正确值的完整数组。 I cannot understand how this is happening. 我不明白这是怎么回事。 Here is my code: 这是我的代码:

function climbingStaircase(n, k) {
  const solutions = [];

  const findSolution = (progressArray, k, remaining) => {
      if (remaining === 0) {
          console.log('pA:', progressArray)
          solutions.push(progressArray);
          return;
      } else {
          for (let i = 1; i <= k; i++){
             if (i <= remaining) {
                progressArray.push(i);
                findSolution(progressArray, k, remaining - i);
                progressArray.pop();
              }
          }
      }
  }
  findSolution([], k, n);
  console.log('final solutions', solutions)
}

With the inputs n = 4 and k = 2, this is what my console looks like: 输入n = 4和k = 2,这就是我的控制台的样子:

pA: [ 1, 1, 1, 1 ]
pA: [ 1, 1, 2 ]
pA: [ 1, 2, 1 ]
pA: [ 2, 1, 1 ]
pA: [ 2, 2 ]
final solutions [ [], [], [], [], [] ]

I've tried assigning interim variables to ensure that it's always referencing the correct array. 我尝试分配临时变量以确保它始终引用正确的数组。 I've tried including my solutions array as a parameter that I pass in... all of which seemed unnecessary, and still returned the same results. 我尝试将我的solutions数组作为我传入的参数包括在内……所有这些似乎都是不必要的,并且仍然返回相同的结果。 I know it's something small that is going to make me roll my eyes, but I cannot see it. 我知道这很小,会让我翻白眼,但我看不到它。 Please help. 请帮忙。

Your code is using one array throughout, and popping all of the entries it pushes into that array. 您的代码在整个过程中都使用一个数组,然后将其压入该数组的所有条目都弹出。 You probably want to save a copy of the array when saving to solutions : 保存到solutions时,您可能想保存阵列的副本

solutions.push(progressArray.slice());
// -------------------------^^^^^^^^

Live Example: 现场示例:

 function climbingStaircase(n, k) { const solutions = []; const findSolution = (progressArray, k, remaining) => { if (remaining === 0) { console.log('pA:', progressArray) solutions.push(progressArray.slice()); return; } else { for (let i = 1; i <= k; i++){ if (i <= remaining) { progressArray.push(i); findSolution(progressArray, k, remaining - i); progressArray.pop(); } } } } findSolution([], k, n); console.log('final solutions', solutions) } climbingStaircase(4, 2); 
 .as-console-wrapper { max-height: 100% !important; } 

Replace solutions.push(progressArray); 替换solutions.push(progressArray); by solutions.push(progressArray.slice()); 通过solutions.push(progressArray.slice()); .

You're passing the progressArray into your function, then you're mutating it. 您将把progressArray传递到函数中,然后对其进行变异。 But since that's always the same reference to progressArray , you're mutating the same array. 但是,由于这始终是对progressArray引用,因此您要对同一数组进行突变。 Eventually, progressArray.pop(); 最终, progressArray.pop(); will remove all elements and you end up with five times the same empty array. 将删除所有元素,最后得到的空数组是相同数组的五倍。

.slice() creates a copy of your array. .slice()创建数组的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM