Discription
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.
Solution
int findMin(vector<int>& nums) {
int index1 = 0, index2 = nums.size() - 1;
int mid = index1;
while (nums[index1] > nums[index2]){
if (index2 - index1 == 1){
mid = index2;
break;
}
mid = (index1 + index2) >> 1;
if (nums[mid] >= nums[index1])
index1 = mid;
else index2 = mid;
}
return nums[mid];
}
int findMin(vector<int> &num) {
int start = 0, end = num.size() - 1;
while (start<end) {
if (num[start]<num[end])
return num[start];
int mid = (start + end) / 2;
if (num[mid] >= num[start])
start = mid + 1;
else
end = mid;
}
return num[start];
}
int findMin(vector<int> &num) {
int start = 0, end = num.size() - 1;
while (start<end){
int mid = (start + end) / 2;
if (num[mid]>num[end]) start = mid + 1;
else end = mid;
}
return num[start];
}