153 find minimum in rotated arra

2017-10-23  本文已影响0人  Fei_JOB

key: this is asking the first element smaller than target = nums[last]

class Solution {
   public int findMin(int[] nums) {
       if(nums == null) return -1;
       int left = 0;
       int right = nums.length -1;
       int target = nums[right];
       while(left + 1 < right){
           int mid = left + (right-left)/2;
           int midVal = nums[mid];
           if(midVal <= target){
               right = mid;
           }else{
               left = mid;
           }
       }
       if(nums[left] < nums[right]) return nums[left];
       else return nums[right];
   }
}
上一篇 下一篇

猜你喜欢

热点阅读