原题地址
https://leetcode.com/problems/find-peak-element/
题目描述
A peak 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.
给出一个不含重复元素的数组num
,找出其中的峰值元素并返回其索引(下标)值.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
这个数组可能含有多个峰值元素,在这种情况下返回任意一个峰值元素的下标都可以.
You may imagine that num[-1] = num[n] = -∞
.
假设nums[-1] = num[n] = -∞
.
For example, in array [1, 2, 3, 1]
, 3
is a peak element and your function should return the index number 2
.
例如,在数组 [1, 2, 3, 1]
中,3
是峰值元素,你的函数需要返回下标2
.
spoilers.
挑战.
Note:
注意:
Your solution should be in logarithmic complexity.
你的方法的时间复杂度需要为对数级别复杂度.