Javascript 递归查找数组中的最大值

Javascript 递归查找数组中的最大值,javascript,recursion,max,Javascript,Recursion,Max,我尝试使用递归在JavaScript中查找数组中的最大值。我创建了这个函数,但不知道如何递归地执行它 function Max(a) { var a = [2,3,5]; return Math.max.apply(Math, a); } 下面是一个递归函数的示例,它接受一个数字数组,比较前两个数字,删除较小的数字,然后在给定数组减去删除的数字后再次调用自身 function max(numArray) { // copy the given array nums

我尝试使用递归在JavaScript中查找数组中的最大值。我创建了这个函数,但不知道如何递归地执行它

function Max(a) {
  var a = [2,3,5];
  return Math.max.apply(Math, a);
}

下面是一个递归函数的示例,它接受一个数字数组,比较前两个数字,删除较小的数字,然后在给定数组减去删除的数字后再次调用自身

function max(numArray) 
{
    // copy the given array 
    nums = numArray.slice();

    // base case: if we're at the last number, return it
    if (nums.length == 1) { return nums[0]; }

    // check the first two numbers in the array and remove the lesser
    if (nums[0] < nums[1]) { nums.splice(0,1); }
    else { nums.splice(1,1); }

    // with one less number in the array, call the same function
    return max(nums);
}
函数最大值(numArray)
{
//复制给定的数组
nums=numArray.slice();
//基本情况:如果我们是最后一个数字,返回它
如果(nums.length==1){返回nums[0];}
//检查数组中的前两个数字并删除较小的数字
if(nums[0]
下面是一个JSFIDLE:

每次调用递归案例时,它都会使它更接近基本案例

max接受两个数字,然后比较它们,然后返回两个数字中较高的一个

每次调用array.shift()时,都会从数组中弹出数组中的第一个元素,因此递归调用中的第二个参数是缩短了一个的数组


当array.length下降到仅一个元素时,返回该元素并观察堆栈展开

这实际上是我向软件职位的应聘者提出的问题之一:在锯齿状数组中查找最大数。数组中的每个元素可以是一个数字,也可以是不定层数的其他数字数组,如下所示:

var ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];
现在,为了好玩和简短起见,我将为这个问题提供两种解决方案。第一个解决方案使用递归,而第二个解决方案使用堆栈。事实上,所有这些递归问题也可以通过使用堆栈方法来解决

解决方案一:使用递归查找最大值
//使用递归查找数组中的最大数值
函数findMax1(ar)
{
var max=-无穷大;
//循环遍历数组的所有元素
对于(变量i=0;i最大值)
{
max=el;
}
}
返回最大值;
}
解决方案二:使用堆栈查找最大值
//使用堆栈查找数组中的最大数值
函数findMax2(arElements)
{
var max=-无穷大;
//这是将放置第一个数组的堆栈,然后
//遍历数组时找到的所有其他子数组
var数组=[];
数组。推送(arelement);
//循环,只要将数组添加到堆栈中进行处理
while(arrays.length>0)
{
//从堆栈中提取数组
ar=arrays.pop();
//…并通过其元素循环
对于(变量i=0;i最大值)
{
max=el;
}
}
}
返回最大值;
}
请随意优化上述代码。您还可以将此代码作为一个

理解“调用堆栈”如何用于递归非常重要

在上面的示例中,由于数组长度超过3,因此要调用的第一行是

const subMax = max(list.slice(1));

这一行所做的只是取出数组中的第一项,并以较短的数组作为参数调用函数。这将一直持续到数组长度为2,然后返回2中的较大值。然后函数就可以完成它以前所有的部分完成状态。

ES6语法使它非常简洁

const findMax = arr => {
  if (!Array.isArray(arr)) throw 'Not an array'
  if (arr.length === 0) return undefined
  const [head, ...tail] = arr
  if (arr.length === 1) return head
  return head > findMax(tail) 
    ? head
    : findMax(tail)
}
试试这个:

const biggestElement = list => {
    if (list.length == 1)
        return list[0];
    if (list[0] > list[1])
        list[1] = list[0];
    list = list.splice(1);
    return biggestElement(list);
}
函数递归MaxNumber(项){
如果(items.length==1){
返回项目[0]//已达到基本大小写
}
否则{
如果(项[0]>=项[items.length-1]){
items.pop()//删除数组中的最后一项
返回递归MaxNumber(项)
}
否则{
return recursiveMaxNumber(items.slice(1))//删除数组中的第一个项
}
}
}

log(recursiveMaxNumber([2,22,3,3,4,44,33])
虽然下面这不是一种有效的方法,但我还是尝试了一下

function max(a, b) {
  return a > b ? a : b
}

function findMaxOfArrayRecursion(arr) {
  if (arr.length == 1) {
    return arr[0]
  }
  return max(arr[0], findMaxOfArrayRecursion(arr.slice(1)))
}
基本上,我们假设第一个数组元素为最大值&以递归方式将其与其他元素进行比较

添加了一个高效的非递归方法,仅供参考

Math.max(...arr)


递归是调用自身的函数。如果函数传递了值数组,那么要传递给内部函数调用的是什么?提示:max(a,b,c)=max(max(a,b,c)。为什么要在函数中设置数组值?Math.max函数在浏览器核心代码中执行递归(或迭代)部分“引擎盖下”。因此,如果你的目标/任务是编写一个“迭代函数”,你可能应该看一看(错误的语言,正确的想法)。这将打破
[1,2,2,3]
(或任何两个相邻值相等的数组),这将破坏原始数组,因此唯一剩余的值是最大的。
const findMax = arr => {
  if (!Array.isArray(arr)) throw 'Not an array'
  if (arr.length === 0) return undefined
  const [head, ...tail] = arr
  if (arr.length === 1) return head
  return head > findMax(tail) 
    ? head
    : findMax(tail)
}
const biggestElement = list => {
    if (list.length == 1)
        return list[0];
    if (list[0] > list[1])
        list[1] = list[0];
    list = list.splice(1);
    return biggestElement(list);
}
function max(a, b) {
  return a > b ? a : b
}

function findMaxOfArrayRecursion(arr) {
  if (arr.length == 1) {
    return arr[0]
  }
  return max(arr[0], findMaxOfArrayRecursion(arr.slice(1)))
}
Math.max(...arr)