Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created February 23, 2020 22:17
Show Gist options
  • Select an option

  • Save wushbin/c2af18e6906bb54c815ec3f2feed78bc to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/c2af18e6906bb54c815ec3f2feed78bc to your computer and use it in GitHub Desktop.
class Solution {
public int findPeakElement(int[] nums) {
int l = 0;
int r = nums.length - 1;
while(l < r) {
int mid = l + (r - l) / 2;
int next = mid + 1; // safe to use mid + 1
if (nums[mid] < nums[next]) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment