Find Peak Element
A peak element is an element that is greater than its neighbors.
Given an input array nums
, where nums[i] ≠ nums[i+1]
, find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞
.
Example 1:
1 | Input: nums = [1,2,3,1] |
Example 2:
1 | Input: nums = [1,2,1,3,5,6,4] |
Note:
Your solution should be in logarithmic complexity.
思路:就按这个题目要求的找到满足当前数大于相邻数的目标数即可,注意首位为峰以及末尾为峰的特殊情况,分情况讨论清楚即可。
java代码如下:
1 | class Solution { |