153. Find Minimum in Rotated Sorted Array

binary search但是三个数的时候单独处理

 1     public int findMin(int[] nums) {
 2         if(nums.length == 0) {
 3             return 0;
 4         }   
 5         int left = 0;
 6         int right = nums.length - 1;
 7         while(left < right - 2) {
 8             if(nums[left] < nums[right]) {
 9                 return nums[left];
10             }
11             int mid = left + (right - left) / 2;
12             if (nums[mid-1] < nums[left]) {
13                 right = mid - 1;
14             } else if (nums[mid+1] > nums[right]){
15                 left = mid + 1;
16             } else {
17                 left = mid - 1;
18                 right = mid + 1;
19             }
20         }
21         return Math.min(Math.min(nums[left + (right - left)/2], nums[left]), nums[right]);
22     }

 

posted @ 2016-06-28 05:25  warmland  阅读(137)  评论(0编辑  收藏  举报