使用递归java在数组中查找最大值 [英] finding max value in an array using recursion java

查看:76
本文介绍了使用递归java在数组中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种递归方法来查找数组中的最大值(我已经知道了迭代方法) 对于基本情况,我想到了这样的想法:

I'm looking for a recursive method to find the maximum value in an array (I know already the iterative one) for the base case, I've came up with the idea that:

if(t.length == 1)
   return t[0];

但是我不知道递归调用步骤 如果有人可以帮助我,我会很高兴

but I don't know about the recursive call step I'll be so glad if anyone could help me

推荐答案

int largest(int[] a, int start, int largest) {
    if (start == a.length)
        return largest;
    else {
        int l = (a[start] > largest ? a[start] : largest);
        return largest(a, start + 1, l);
    }
}

这篇关于使用递归java在数组中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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