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.
Subscribe to see which companies asked this question.
public class Solution {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right])
left = mid+1;
else
right = mid;
}
return nums[left];
}
}
本文介绍了一种在已知无重复元素的旋转有序数组中查找最小元素的有效算法。通过二分查找的方法,在O(log n)的时间复杂度内确定了最小元素的位置。
354

被折叠的 条评论
为什么被折叠?



