学习记录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);
}
}

浙公网安备 33010602011771号