/**
* binary search approach, o(logN)
*/
public class Solution {
public int findMin(int[] nums) {
int low = 0, high = nums.length-1, mid;
while (low < high) {
mid = (low+high)/2;
// the smallest must be in the right side
// and nums[mid] cannot be the smallest, thus let low = mid+1 (*)
if (nums[mid] > nums[high])
low = mid+1;
// the smallest must be in the left side
// nums[mid] could be the samllest, thus let high = mid (*)
else
high = mid;
}
return nums[low];
}
}
Leetcode 153. Find Minimum in Rotated Sorted Array
最新推荐文章于 2017-01-09 13:17:35 发布