← 返回 meta 的题目列表Find Peak Element Index in Array with Logarithmic Complexity
类型:online_judge
meta
You need to implement a function to find a peak element in an array. A peak element is an element that is greater than its neighbors. Given a 1-dimensional integer array nums, where all integers are unique and at least one peak is guaranteed, write a method to find the peak element and return its index. You may assume that nums[-1] = nums[n] = -∞. Your solution should have logarithmic complexity.
Example Input/Output:
Input: nums = [1, 2, 3, 1]
Output: 2
Constraints:
1 <= nums.length <= 1000
-2^31 <= nums[i] <= 2^31 - 1
nums[i] ≠ nums[i+1]
Example
Input
3
1 2 3 1