二分查找一>山脉数组的峰顶索引

2024-10-09 16:01:35 浏览数 (5)

1.题目: 


2.解析:

代码:

代码语言:javascript复制
public int peakIndexInMountainArray(int[] arr) {
        int left = 1, right = arr.length-2;

        while(left < right) {
            int mid = left   (right-left 1) / 2;
            if(arr[mid] > arr[mid-1]) left = mid;
            else  right = mid-1;
        }
        return left;
    }

0 人点赞