Middle-题目40:162. Find Peak Element

原创 2016年05月31日 16:01:52

题目原文:
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.
题目大意:
寻找一个数组的峰值元素(类似于函数中的极大值)
假设num[-1]=num[n]=- ∞。
若数组中有多个峰值元素,返回任意一个都可以。
题目分析:
本题依然是一个二分查找的变形。先考虑边界情况,若数组只有1个元素,则他就是峰值;若数组只有两个元素则较大的就是峰值元素。
接下来进行二分查找,设中点为mid,考虑以下情况:
(1) mid是端点,那么只看它是否大于第二个点(倒数第二个点)即可;
(2) mid点的值大于左边又大于右边,则它就是峰值,返回即可;
(3) mid点的“邻域”是递增的,则右半边一定有峰值;
(4) mid点的“邻域”是递减的,则左半边一定有峰值;
源码:(language:c)

int findPeakElement(int* nums, int numsSize) {
    if(numsSize == 1) 
        return 0;
    else if(numsSize == 2)
        return nums[0]<nums[1];
    else {
        int mid=(numsSize-1)/2;
        if(mid == 0 && nums[0]>nums[1])
            return 0;
        else if(mid == numsSize-1 && nums[numsSize-1] > nums[numsSize-2])
            return numsSize-1;
        else if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1])
            return mid;
        else if(nums[mid] >= nums[mid-1] && nums[mid] <= nums[mid+1])
            return findPeakElement(&nums[mid+1],numsSize-mid-1)+mid+1;
        else 
            return findPeakElement(nums,numsSize-mid-1);
    }
}

成绩:
0ms,beats 9.38%,众数0ms,90.62%
cmershen的碎碎念:
二分查找不一定只适用于有序的数组。像本题的数组是不知道顺序的,但适用二分查找。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

<LeetCode OJ> 162. Find Peak Element

162. Find Peak ElementMy SubmissionsQuestion Total Accepted: 56981 Total Submissions: 174002 Difficu...

Leetcode 162 Find Peak Element 查找峰值元素(极大值)

给出一个不含重复元素的数组num,找出其中的峰值元素(极大值)并返回其索引(下标)值....

LeetCode - 162. Find Peak Element

题目中给出了一个很重要的假设,就是数组左侧左边和右侧右边的数都是负无穷大,所以我们可以采用一个很方便的算法进行计算。 我们从index为1的时候开始扫描整个数组,比较nums[i]和nums[i -...

【LeetCode】 162. Find Peak Element

162. Find Peak Element介绍

LeetCode 162. Find Peak Element

LeetCode 162. Find Peak Element 两种办法

LeetCode(162) Find Peak Element

题目如下: A peak element is an element that is greater than its neighbors. Given an input array where nu...

【leetcode】162. Find Peak Element

Difficulty:medium A peak element is an element that is greater than its neighbors. Given...

Find Peak Element - LeetCode 162

题目描述: A peak element is an element that is greater than its neighbors. Given an input array where nu...
  • bu_min
  • bu_min
  • 2015年05月16日 21:22
  • 216

[LeetCode]162.Find Peak Element

【题目】 A peak element is an element that is greater than its neighbors. Given an input array where ...

LeetCode(162) Find Peak Element

题目A peak element is an element that is greater than its neighbors.Given an input array where num[i] ...
  • fly_yr
  • fly_yr
  • 2015年12月05日 14:08
  • 475
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目40:162. Find Peak Element
举报原因:
原因补充:

(最多只允许输入30个字)