Leetcode brush question record 2

Index of the peak of the mountain array

Insert picture description here

class Solution {
    
    
  public int peakIndexInMountainArray(int[] A) {
    
    
    // 1.[定左右]
    int l = 0;
    int r = A.length-1;
	
    // 2.[定区间]
    while (l <= r) {
    
    // [l,r]
        // 3.[取中值]
        int mid = l + (r-l)/2;
       
        // 4.[进退]
        if (A[mid+1] > A[mid]) {
    
    // 上坡
            l = mid + 1; // [爬坡]
        } else if (A[mid-1] > A[mid]){
    
    // 下坡
            r = mid - 1; // [返回坡顶]
        } else {
    
    
            return mid;
        }

    }
	
    // 5.[无功而返]
    return -1;

}

Guess you like

Origin blog.csdn.net/as1490047935/article/details/115380088