注:此博客不再更新,所有最新文章将发表在个人独立博客limengting.site。分享技术,记录生活,欢迎大家关注
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.
// Binary Search
class Solution {
public int findMin(int[] nums) {
int start = 0, end = nums.length - 1;
while (start < end) {
if (nums[start] < nums[end]) break;
int mid = (start + end) / 2;
if (nums[mid] >= nums[start]) // Attention: >=, counterexample:[2,1]
start = mid + 1;
else end = mid;
}
return nums[start];
}
}