问题描述:https://leetcode.cn/problems/find-peak-element/
峰值元素是指其值严格大于左右相邻值的元素。
给你一个整数数组 nums
,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞
。
你必须实现时间复杂度为 O(log n)
的算法来解决此问题。
方法一:找出最大值,but因为不知道数组是不是有序的,所以你需要遍历数组,时间复杂度上为0(n),不满足要求,虽然吧,我也不知道为啥会过
class Solution {
public int findPeakElement(int[] nums) {
int id=0;
for(int i=1;i<nums.length;i++){
if(nums[i]>nums[id])id=i;//每次循环选出最大的索引赋给id;
}return id;
}
}
方法二爬坡,从力扣官网瞅到的
记一次不成功的改造
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int idx = (int) (Math.random() * n);//调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范围是[0.0,1.0)的左闭右开区间,返回值是一个伪随机选择的数,在使用int强制转换一下就可以调出数组区间内的任意整数了
while (!(compare(nums, idx - 1, idx) < 0 && compare(nums, idx, idx + 1) > 0)) {
if (compare(nums, idx, idx + 1) < 0) {
idx += 1;
} else {
idx -= 1;
}
}
return idx;
}/*public int compare(int []arr,int a,int b){
int n=arr.length;
if(a==n||a==-1||b==n||b==-1){
return 0;
}else if(arr[a]==arr[b])return 0;
else return arr[a]>arr[b]?1:-1;
}*/
//why上面这么写不成立,一直报数组下标越界
}
不知道官网的get方法有什么特殊含义,就没搞进去,结果一直报数组下标越界,脑壳痛
下面是官网给出的迭代爬坡
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int idx = (int) (Math.random() * n);
while (!(compare(nums, idx - 1, idx) < 0 && compare(nums, idx, idx + 1) > 0)) {//找到峰值之前继续循环
if (compare(nums, idx, idx + 1) < 0) {//说明右边大
idx += 1;
} else {//反之左边大,idx向右移
idx -= 1;
}
}
return idx;//最后idx即为其中之一的峰值索引
}
// 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
// 方便处理 nums[-1] 以及 nums[n] 的边界情况
public int[] get(int[] nums, int idx) {
if (idx == -1 || idx == nums.length) {
return new int[]{0, 0};
}
return new int[]{1, nums[idx]};///不太理解为什么返回一个数组
}
public int compare(int[] nums, int idx1, int idx2) {
int[] num1 = get(nums, idx1);
int[] num2 = get(nums, idx2);
if (num1[0] != num2[0]) {
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/find-peak-element/solutions/998152/xun-zhao-feng-zhi-by-leetcode-solution-96sj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这个时间复杂度也是o(n)
10小时前的是方法一的数据,剩下的两个是方法二的数据
这位仁兄的方法看着就很妙,代码简单,运行结果也很好
class Solution {
public int findPeakElement(int[] nums) {
int l = 0, r = nums.length - 1;
while (l < r) {
int mid = l + ((r - l) >> 1);
if (nums[mid] < nums[mid+1]) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}
}
方法三:二分法查找峰值
class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int left = 0, right = n - 1, ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (compare(nums, mid - 1, mid) < 0 && compare(nums, mid, mid + 1) > 0) {
ans = mid;
break;
}
if (compare(nums, mid, mid + 1) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}
// 辅助函数,输入下标 i,返回一个二元组 (0/1, nums[i])
// 方便处理 nums[-1] 以及 nums[n] 的边界情况
public int[] get(int[] nums, int idx) {
if (idx == -1 || idx == nums.length) {
return new int[]{0, 0};
}
return new int[]{1, nums[idx]};
}
public int compare(int[] nums, int idx1, int idx2) {
int[] num1 = get(nums, idx1);
int[] num2 = get(nums, idx2);
if (num1[0] != num2[0]) {
return num1[0] > num2[0] ? 1 : -1;
}
if (num1[1] == num2[1]) {
return 0;
}
return num1[1] > num2[1] ? 1 : -1;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/find-peak-element/solutions/998152/xun-zhao-feng-zhi-by-leetcode-solution-96sj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这个算法有一丢丢小懵逼,我选择run去睡觉