153. Find Minimum in Rotated Sorted Array
class Solution {
public int findMin(int[] nums) {
// if array is rotated, the first element will always larger than the last elememt
// binary search
return binarySearch(nums, 0, nums.length);
}
private int binarySearch(int[] nums, int start, int end){
//end is the not searchaable
int mid = (start+end)/2;
if (mid==0 || mid==nums.length || nums[mid-1]>nums[mid]) return nums[mid%nums.length];
if (nums[mid]>nums[0]) return binarySearch(nums, mid+1, end);
else return binarySearch(nums, start, mid);
}
}Last updated