Leetcode 162 Python

思路:使用二分法,若mid > mid+1 说明0-mid必然有一个peak,否则mid-len(nums)必然有一个peak。

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

你可能感兴趣的:(Leetcode)