## 题目描述：

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.

Note:

Your solution should be in logarithmic complexity.

“峰值元素”是指值大于邻居的元素

## 解题思路：

Brute Force暴力枚举，线性复杂度 O（n）

## Python代码：

``````class Solution:
# @param num, a list of integer
# @return an integer
def findPeakElement(self, num):
size = len(num)
return self.search(num, 0, size - 1)

def search(self, num, start, end):
if start == end:
return start
if start + 1 == end:
return [start, end][num[start] < num[end]]
mid = (start + end) / 2
if num[mid] < num[mid - 1]:
return self.search(num, start, mid - 1)
if num[mid] < num[mid + 1]:
return self.search(num, mid + 1, end)
return mid
``````

``````class Solution:
# @param num, a list of integer
# @return an integer
def findPeakElement(self, num):
size = len(num)
for x in range(1, size - 1):
if num[x] > num[x - 1] and num[x] > num[x + 1]:
return x
return [0, size - 1][num[0] < num[size - 1]]
``````

Pingbacks已关闭。