153. Find Minimum in Rotated Sorted Array

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.

 

给定一个升序(没有重复数字),但是被移动了N位,获取最小值

思路:就是找到升序队列的第一个元素

 1     public int findMin(int[] nums) {
 2         if(nums.length == 1) return  nums[0];
 3         int low = 0,high=nums.length  -1;
 4         while (low < high)
 5         {
 6             int mid = (low + high)/2;
 7             if (nums[mid] > nums[high]) low = mid +1;
 8             else high = mid; 
 9         }
10         return nums[low];       
11     }

 扩展: 在移动了N位的数组中找出目标数的索引位置 33. Search in Rotated Sorted Array

posted @ 2017-10-16 16:10  daniel456  阅读(101)  评论(0编辑  收藏  举报