用递归的方式求数组最大值

阅读 226

2022-03-11

Java代码如下:

public static void main(String[] args) {
	int[] testArray = new int[]{1,2,3,4,5,6,7,7,10,22,7,5,2,3,5,5};
        System.out.printf(""+process(testArray,0,testArray.length-1));
    }

    public static int process(int[] arr,int L,int R){
        if (L==R){
            return arr[L];
        }
        int mid = L + ((R-L)>>1);
        int leftMax = process(arr,L,mid);
        int rightMax = process(arr,mid+1,R);
        return Math.max(leftMax,rightMax);
    }

精彩评论(0)

0 0 举报