# LeetCode162. Find Peak Element

97人阅读 评论(0)

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.

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] = -∞.

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

Note:

Your solution should be in logarithmic complexity.

    static  int findPeakElementImp(int[] array, int s , int e){
if(e-s <= 1){
return array[e]> array[s]?e:s;
}
int mid = s+ (e-s)/2;
if(array[mid] > array[mid -1] && array[mid]> array[mid+1]){
return  mid;
}
if( array[mid] > array[mid+1]){
return findPeakElementImp(array ,s, mid);
}else{
return  findPeakElementImp(array, mid ,e);
}
}
static  public  void main(String[] args ){
int[] testArray ={1,2,6};
int result = findPeakElementImp(testArray, 0, testArray.length -1 );
System.out.println(result);
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：41481次
• 积分：1621
• 等级：
• 排名：千里之外
• 原创：125篇
• 转载：0篇
• 译文：0篇
• 评论：24条
阅读排行
最新评论