The ticky part for this question is :
mid == 0 num[mid] < num[mid+1] start = mid + 1;
0 < mid < len num[mid-1] < num[mid] < num[mid+1] start = mid + 1;
0 < mid < len num[mid-1] > num[mid] > num[mid+1] end = mid - 1;
mid = l num[mid] < num[mid-1] end = mid - 1;
But after we do the comparison, we can simplify the condition:
mid == 0 || mid < length-1 && num[mid] < num[mid + 1] start = mid + 1
mid == length-1 || mid > 0 && num[mid] > num[mid - 1] end = mid - 1
1 class Solution { 2 public: 3 int findPeakElement(const vector &num) { 4 int start = 0, end = num.size()-1, mid = 0; 5 while (start <= end) { 6 mid = (start + end)/2; 7 if ((mid == 0 || num[mid] > num[mid-1]) && 8 (mid == num.size()-1 || num[mid] > num[mid+1])) { 9 return mid;10 } else if (mid == 0 || (mid < num.size()-1 && num[mid] < num[mid+1])){11 start = mid + 1;12 } else {13 end = mid - 1;14 }15 }16 }17 };