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.
The array may contain duplicates.
题目很简单,思路跟Search in Rotated Sorted Array差不多,只是这题可以不用二分查找了。
public class Solution {
public int findMin(int[] nums) {
int nlen = nums.length;
int i;
boolean flag = false; // not rotated
for(i=1;i<nlen;i++)
{
if(nums[i]>=nums[i-1])
continue;
else
{
flag = true; // rotated
break;
}
}
if(flag==false)
return nums[0];
else
return nums[i];
}
}