学习记录24

递归方式
public class MaxInArrayRecursive {
public static void main(String[] args) {
int[] array = {12, 45, 67, 23, 9, 55};
int max = findMaxRecursive(array, 0, array.length - 1);
System.out.println("数组中的最大值是: " + max);
}

public static int findMaxRecursive(int[] arr, int start, int end) {
    if (start == end) {
        return arr[start];
    }
    int mid = (start + end) / 2;
    int leftMax = findMaxRecursive(arr, start, mid);
    int rightMax = findMaxRecursive(arr, mid + 1, end);
    return Math.max(leftMax, rightMax);
}

}

posted @ 2025-02-20 19:58  吉尼泰梅  阅读(8)  评论(0)    收藏  举报