Peak Index in a Mountain Array
给一个数组, 找一个index,比左大比右小, 这个题前提是这个index一定存在, 省去很多corn cases的判断, 直接二分找
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public int peakIndexInMountainArray(int[] A) { int start = 0, end = A.length - 1; while (start < end) { int mid = start + (end - start) / 2; if (A[mid] < A[mid + 1]) start = mid + 1; else end = mid; } return start; } } |