class Solution {
public:
int findPeakElement(const vector<int> &num) {
int low = 0, high = num.size()  1;
while (low < high  1) {
int mid = (low + high) / 2;
if (num[mid] > num[mid  1] && num[mid] > num[mid + 1])
return mid;
else if (num[mid] > num[mid + 1])
high = mid  1;
else
low = mid + 1;
}
return num[low] > num[high] ? low : high;
}
};
Sharing a more standard Binary Search C++ Solution


First, this case {1, 2, 1, 1, 1, 1} is not RIGHT. It does NOT satisfy "Given an input array where num[i] ≠ num[i+1]" , and with that requirement together with this "num[n] & num[1] = ∞", they're very important. They guarantee that the peak could always exist and then guarantee the correctness of binary search for this problem.