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.
思路:二分法,每次看中点是在rotate后的哪一段,然后移动left或right端点。
public int findMin(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int left = 0, right = nums.length - 1;
while (left + 1 < right) {
int middile = left + (right - left) / 2;
if (nums[middile] > nums[right]) {
left = middile;
} else {
right = middile;
}
}
return Math.min(nums[left], nums[right]);
}