# [leetcode] Find Peak Element

#### 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.

Note:Your solution should be in logarithmic complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

C++代码：

```class Solution {
public:
int _findPeakElement(const vector<int> &num, int start, int end){
//vector[end] is not included in the possible solution field.
if(end - start == 1){
return start;
}
int mid = (end + start) / 2;
int n = num.size();
if(mid - 1 != -1 && num[mid - 1] > num[mid]){
return _findPeakElement(num, start, mid);
}
else if(mid + 1 != n && num[mid + 1] > num[mid]){
return _findPeakElement(num, mid + 1, end);
}
else{
return mid;
}
}
int findPeakElement(const vector<int> &num) {
int n = num.size();
if(n == 0){
return 0;
}
return _findPeakElement(num, 0, n);
}
};```

This site uses Akismet to reduce spam. Learn how your comment data is processed.