题目如下:
Given an array of integers. Find a peak element in it. An array element is peak if it is larger than its neighbors. For corner elements, we need to consider only one neighbor. For example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15, 2, 23, 90, 67}, there are two peak elements: 20 and 90. Note that we need to return any one peak element.
分析如下:
面试题目,来自G。首先,这道题目一定可以有一个答案。
(1)如果数组先增大再减小,一定有答案。
(2)如果数组单调递增,最后一个元素就是答案。
(3)如果数组单调递简,第一个元素就是答案。
(4)如果数组全部都一样,任何一个元素都是答案,更极端地,如果数组只有一个元素,那么这唯一的元素就是答案。
(5)如果数组出现了几个峰值,那么任何一个峰值都是答案。
normal case:
二分一次找到中点,如果中点Array[mid]比Array[mid-1]和Array[mid+1]都大,那么就返回mid。否则,如果Array[mid]>Array[mi