作为递归存储的参数-使用从startNum到endNum的所有值的递归返回一个数组 [英] Parameters as storage in recursion - Return an array using recursion of all values from startNum to endNum

查看:59
本文介绍了作为递归存储的参数-使用从startNum到endNum的所有值的递归返回一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习递归时,我遇到了这个问题,并且已经解决了,但是不是我想要的方式.在我的解决方案中,我将值存储在全局范围内声明的数组中.

In learning about recursion, I've come across this problem and I've solved it, but not in the way I would like. In my solution, I'm storing values within an array declared in global scope.

这是我的代码:

//I don't want array in global scope, but within the function
let array = [];

const rangeOfNumbers = (startNum, endNum) => {

  if (startNum === endNum) {
    array.splice(0, 0, startNum);
    return array;
  }

  {
    array.splice(0, 0, endNum);
    // console.log(array)
    const newEndNum = endNum - 1;
    endNum = newEndNum;
    return rangeOfNumbers(startNum, endNum);

  }

}

console.log(rangeOfNumbers(6, 10))

我尝试将其作为参数包含 const rangeOfNumbers =(startNum,endNum,array = []),但是每次调用该函数时都会导致重新创建数组,所以当我以递归方式调用该函数,因此返回的值是单个值 [6] 的数组.当我在函数范围内声明可变变量时,会观察到相同(不必要的)行为

I've tried including it as an argument const rangeOfNumbers = (startNum, endNum, array = []) but that leads to the recreation of an array every time the function is called, so when I call the function recursively, so the returned value is an array of a single value [6]. The same (undesired) behavior is observed when I declare a mutable variable within the function scope

const rangeOfNumbers = (startNum, endNum) => {

  let array = [];

  if(startNum === endNum) {...

据我了解,拼接方法在这里应该不是问题,因为它不会删除任何元素.

As far as I understand, the splice method should not be an issue here, since it isn't deleting any elements.

通常,每当我尝试递归解决问题时,我都难以理解如何制定最简单的基本案例.因此,我有一种感觉-尽管我认为基本逻辑使我很困惑.

In general, I'm having trouble understanding how to formulate the easiest base case whenever I try to solve problems recursively. So I have a feeling that - even though I think the base logic is screwing me.

指向可以提供提示的人,而不是简单地提供答案,而这些提示可以使人们更清楚地思考这一点.

Points to anyone who can offer hints on how to think of this more clearly, rather than simply offering the answer.

谢谢!

推荐答案

检查 startNum endNum 是否相等.如果返回,则返回 [startNum] .如果不是,则返回 [startNum] 并将其与调用 rangeOfNumbers 并以 startNum 递增1:

Check if the startNum and endNum are equal. If they are return [startNum]. If not, return [startNum] and concat to it the result of calling rangeOfNumbers with startNum incremented by 1:

const rangeOfNumbers = (startNum, endNum) =>
  startNum === endNum ?
    [startNum]
    :
    [startNum].concat(rangeOfNumbers(startNum + 1, endNum));

console.log(rangeOfNumbers(6, 10))

另一种非功能性方法(对 arr 进行了更改)是创建一个保存数组的函数,并在其中运行递归:

Another non functional approach (the arr is mutated) is to create a function that holds the array, and run the recursion inside it:

const rangeOfNumbers = (startNum, endNum) => {
  const arr = [];
  
  const fn = (startNum, endNum) => {
    arr.push(startNum);
  
    if(startNum !== endNum) fn(startNum + 1, endNum);
  };
  
  fn(startNum, endNum);
  
  return arr;
}

console.log(rangeOfNumbers(6, 10))

这是基于@ThankYou解决方案的双向方法.主要区别在于它使用(b-a)/Math.abs(b-a)计算步长-1/-1.

This is a bi-directional approach based on @ThankYou's solution. The main difference is that it uses (b - a) / Math.abs(b - a) to calculate the step - 1 / -1.

const range = (a, b, r = []) =>
  a === b
    ? [...r, a]
    : range(a + (b - a) / Math.abs(b - a), b, [...r, a])

console.log(JSON.stringify(range(0,7)))   // [0,1,2,3,4,5,6,7]
console.log(JSON.stringify(range(3,9)))   // [3,4,5,6,7,8,9]
console.log(JSON.stringify(range(9,3)))   // [9,8,7,6,5,4,3]
console.log(JSON.stringify(range(-3,-7))) // [-3,-4,-5,-6,-7]
console.log(JSON.stringify(range(-7,-3))) // [-7,-6,-5,-4,-3]
console.log(JSON.stringify(range(2,-2)))  // [2,1,0,-1,-2]
console.log(JSON.stringify(range(-2,2)))  // [-2,-1,0,1,2]

这篇关于作为递归存储的参数-使用从startNum到endNum的所有值的递归返回一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆