Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Find the minimum element.
You may assume no duplicate exists in the array.
public int findMin(int[] nums) { int left = 0, right = nums.length-1; while(left<right && nums[left]>nums[right]){ int mid = (left + right)/2; if (nums[mid]<nums[right]) { right = mid; }else { left = mid + 1; } } return nums[left]; }
public int findMin(int[] nums) { int min = 0, low = 0, high = nums.length-1; while(low<=high){ if (nums[low]>nums[high]) { while(nums[high--]>nums[high]){ continue; } min = nums[high + 1]; break; }else { min = nums[low]; break; } } return min; }