[leedcode 162] Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

click to show spoilers.

public class Solution {
    public int findPeakElement(int[] nums) {
        //利用抛物线的知识,因为nums[-1]=nums[n]=-∞,所以抛物线存在极值点
        //传统的办法时间复杂度O(n),二分法时间复杂度O(lgn)
        //分别就mid处在极值点左边还是右边进行分开处理,注意递归的终止条件——是只剩两个点
        if(nums==null||nums.length<=0) return -1;
        int res=find(nums,0,nums.length-1);
        return res;
    }
    public int find(int[] nums,int start,int end){
        if(end-start<=1){
            return nums[start]>nums[end]?start:end;
        }
        int mid=(start+end)/2;
        if(nums[mid]>nums[mid+1]&&nums[mid]>nums[mid-1])return mid;
        if(nums[mid]<nums[mid+1]) return find(nums,mid+1,end);
        if(nums[mid]>nums[mid+1]) return find(nums,start,mid-1);
        return -1;
    }
}
时间: 2024-11-09 09:35:19

[leedcode 162] Find Peak Element的相关文章

LeetCode OJ 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

162. Find Peak Element (Array; Divide-and-Conquer)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

Leetcode 162 Find Peak Element (二分查找思想)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

162.Find Peak Element(二分查找)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to anyone of the peaks is fin

leetcode[162]Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

162. Find Peak Element

题目: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks i

leetcode 162. Find Peak Element --------- java

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

LeetCode 162. Find Peak Element (找到峰值)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

Java for LeetCode 162 Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi