题解 | 寻找峰值
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
public int findPeakElement (int[] nums) {
if (nums.length == 0) {
return -1;
}
if (nums.length == 1) {
return 0;
}
// write code here
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
if ( nums[0] > nums[1]) {
return 0;
}
} else if (i == nums.length - 1) {
if (nums[i] > nums[i - 1]) {
return nums.length - 1;
}
} else {
if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
return i;
}
}
}
return -1;
}
}
暴力解法,没什么好说的,最好不要用

