题解 | #寻找峰值#
寻找峰值
http://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findPeakElement (int[] nums) {
if(nums.length==1 || nums[0]>nums[1]){
return 0;
}
if(nums[nums.length-1]>nums[nums.length-2]){
return nums.length-1;
}
int s = 0;
int e = nums.length-1;
int c = 0;
while(s<=e){
c = (s+e)>>1;
if(c==0&&nums[c]>nums[e]){
return c;
}else if(c==0&&nums[c]<nums[e]){
return e;
}
if(c==nums.length-1&&nums[c]>nums[s]){
return c;
}else if(c==nums.length-1&&nums[c]<nums[s]){
return c;
}
if(nums[s]<nums[s+1]&&nums[c]<=nums[c-1]){
e = c - 1;
}else if(nums[e]<nums[e-1] && nums[c]<=nums[c+1]){
s = c + 1;
}else{
return c;
}
}
return e;
}
}