LeetCode 162 Find Peak Element (Python)

Posted by 小明MaxMing on November 2, 2024

题目

A peak element is an element that is strictly greater than its neighbors.

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

You must write an algorithm that runs in O(log n) time.

解题思路

代码

class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        l, r = 0, len(nums) - 1
        while l <= r:
            if l == r:
                return l
            m = (l + r) // 2
            if nums[m - 1] < nums[m] > nums[m + 1]:
                return m
            if nums[m] < nums[m + 1]:
                l = m + 1
            else:
                r = m - 1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅