假设一个按照升序排列的有序数组从某未知的位置旋转。
(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2)。
找到其中最小的元素。
你可以假设数组中不存在重复的元素。
详见:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
Java实现:
class Solution {
public int findMin(int[] nums) {
int low=0;
int high=nums.length-1;
int mid=0;
while(nums[low]>=nums[high]){
if(high-low==1){
mid=high;
break;
}
mid=(low+high)>>1;
if(nums[low]==nums[mid]&&nums[mid]==nums[high]){
int mn=nums[low];
for(int i=low+1;i<=high;++i){
if(mn>nums[i]){
mn=nums[i];
}
}
return mn;
}else if(nums[low]<=nums[mid]){
low=mid;
}else if(nums[mid]<=nums[high]){
high=mid;
}
}
return nums[mid];
}
}