Middle-题目40：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.

（1） mid是端点，那么只看它是否大于第二个点（倒数第二个点）即可；
（2） mid点的值大于左边又大于右边，则它就是峰值，返回即可；
（3） mid点的“邻域”是递增的，则右半边一定有峰值；
（4） mid点的“邻域”是递减的，则左半边一定有峰值；

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(162) Find Peak Element

2015-01-01 16:36:25

Leetcode: Find Peak Element

2014-12-17 23:55:37

LeetCode162——Find Peak Element

2015-08-03 14:36:24

leetcode_c++：Find Peak Element（162）

2016-06-14 23:44:23

LeetCode-162. Find Peak Element (JAVA)寻找peak元素

2017-04-28 14:29:29

【LEETCODE】162-Find Peak Element [Python]

2016-01-08 19:45:05

162. Find Peak Element

2015-08-25 15:42:55

162. Find Peak Element**

2017-01-10 10:51:47

Find Peak Element --leetcode

2014-12-15 14:25:39

LeetCode 162. Find Peak Element（查找峰值）

2016-05-25 01:40:48