153. Find Minimum in Rotated Sor

2017-01-09  本文已影响0人  juexin

Suppose an array sorted in ascending order 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) {
        
        int start = 0,end = nums.length-1;
        while(nums[start]>nums[end])  //注意限制条件!!!
        {
            int mid = (start + end)>>1;
            System.out.println(nums[mid]);
            System.out.println(nums[start]);
            if(nums[mid]>=nums[start])
               {
                   start = mid +1;
                   System.out.println(start);
               }
            else 
               {
                   end = mid;
              //     System.out.println(end);
               }
        }
        return nums[start];//注意这个神奇的start或者left!!!
    }
}
上一篇下一篇

猜你喜欢

热点阅读