我是靠谱客的博主 正直裙子,这篇文章主要介绍用递归的方式找出数组中的最大值,现在分享给大家,希望可以做个参考。

package com;
//用递归的方式找出数组中的最大值
public class Demo {
public static void main(String[] args) {
int[] arr={1,2,3,5,1,41,7,3};
System.out.println(getMaxValue(arr, 0, arr.length-1));
}
public static int getMaxValue(int[] arr,int left,int right) {
//不需要再划分的条件是(终止条件):自己就是自己范围上的最大值
if (left==right) {
return arr[right];
}
//中间位置
int mid=(left+right)/2;
//递归求得左边部分的最大值
int maxLeft=getMaxValue(arr, left, mid);
//递归求得右边部分的最大值
int maxRight=getMaxValue(arr, mid+1, right);
//返回最大的
return Math.max(maxLeft, maxRight);
}
}

最后

以上就是正直裙子最近收集整理的关于用递归的方式找出数组中的最大值的全部内容,更多相关用递归内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(130)

评论列表共有 0 条评论

立即
投稿
返回
顶部