题目
题解
如输入[2,4,1,2,7,8,4]时,会形成两个山峰,一个是索引为1,峰值为4的山峰,另一个是索引为5,峰值为8的山峰,如下图所示:
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findPeakElement (int[] nums) {
// write code here
if (nums == null) {
return -1;
}
int[] dum = new int[nums.length+2];
dum[0] = Integer.MIN_VALUE;
dum[dum.length-1] = Integer.MIN_VALUE;
for(int i = 0; i < nums.length; i++) {
dum[i+1] = nums[i];
}
int left = 0;
int mid = 1;
int right = 2;
while (right < dum.length) {
if (dum[left] < dum[mid] && dum[mid] > dum[right]) {
return mid-1;
}
left++;
mid++;
right++;
}
return -1;
}
}
标签:right,dum,nums,19,++,mid,int,必刷,TOP101
From: https://blog.51cto.com/u_16244372/8172957