时间复杂度要求O(logN),说明得用二差查找。如何用呢?
- 检查中间的值是否满足峰值要求,是的话返回;否的话会形成俩个子数组。通过递归遍历,得到最终的值
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
int resultNumber;
public int findPeakElement (int[] nums) {
// write code here
if(nums==null||nums.length==0){
return 0;
}
test(nums,0,nums.length-1);
return resultNumber;
}
public void test(int[] nums,int left,int right){
if(left<=right && left>=0){
int center =(left+right)/2;
int centerValue=nums[center];
int vLeft;
if(center-1>=0){
vLeft =nums[center-1];
}else{
vLeft= Integer.MIN_VALUE;
}
int vRight;
if(center+1<=right){
vRight =nums[center+1];
}else{
vRight = Integer.MIN_VALUE;
}
if(centerValue>vLeft&¢erValue>vRight){
resultNumber=center;
return;
}
test(nums,left,center-1);
test(nums,center+1,right);
}
}
}