题目地址:https://leetcode.com/problems/find-peak-element/description/

题目描述:

Apeak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

Thearray may contain multiple peaks, in that case return the index to any one of the peaks is fine.

Youmay imagine that num[-1] = num[n] = -∞.

Forexample, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

Note: Your solution should be in logarithmic complexity.

Credits: Special thanks to @ts for adding this problem and creating all test cases.

题目大意

找到数组中的一个山峰节点位置,返回这个节点的位置。

解题方法

用两个mid,判断上坡还是下坡~二叉搜索,在这种题目中很常用~~

代码:

class Solution(object):
    def findPeakElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        left, right = 0, len(nums) - 1
        while left < right:
            mid1 = (left + right) / 2
            mid2 = mid1 + 1
            if nums[mid1] < nums[mid2]:
                left = mid2
            else:
                right = mid1
        return left

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发