[leedcode 153] Find Minimum in Rotated Sorted Array
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 class Solution { public int findMin(int[] nums) { //将数组分成两个部分,left不断逼近right,当二者挨着时,right指向的是小的 //注意:递增数组分开讨论!! if(nums==null||nums.length<=0) return -1; int left=0; int right=nums.length-1; if(nums[left]<nums[right])return nums[left];//递增数组 while(left<right-1){ int mid=(left+right)/2; if(nums[mid]>nums[left]){ left=mid; }else{ right=mid; } } return nums[right]; } }