162 Find Peak Element – Medium
Problem:
Thoughts:
Solutions:
public class Solution {
public int findPeakElement(int[] nums){
if (nums == null)
return -1;
if (nums.length == 1)
return 0;
int start = 0;
int end = nums.length - 1;
while (start <= end){
int mid = (start + end) /2;
if ((mid == 0 || nums[mid - 1] < nums[mid]) && (mid == nums.length - 1 || nums[mid] > nums[mid + 1]))
return mid;
else if (mid > 0 && nums[mid - 1] > nums[mid])
end = mid - 1;
else
start = mid + 1;
}
return -1;
}
}Last updated