Leetcode每日一题2021/01/19

Leetcode每日一题2021/01/19_第1张图片
Leetcode每日一题2021/01/19_第2张图片
3

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left, right = 0, len(arr)-1
        while(left <= right):               # [0, len(arr)-1]
            mid = left + (right - left)//2
            if(arr[mid] < arr[mid+1]):      # 上升趋势
                left = mid + 1
            elif(arr[mid-1] > arr[mid]):    # 下降趋势
                right = mid - 1
            elif(arr[mid-1] < arr[mid] > arr[mid+1]):
                return mid
        return -1

Leetcode每日一题2021/01/19_第3张图片

你可能感兴趣的:(Leetcode,leetcode,python,算法)