Suppose a sorted array 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.

Hide Tags

Array Binary Search

 

  二分查找的变形,比较神奇的是数列可能没有旋转,查找前判断下数列旋转了没有便可以应对。
 
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
if(n<||num[]<num[n-]) return num[];
int lft=,rgt=n-,mid=n-;
while(lft+<rgt){
if(num[mid-]>num[mid]) break;
mid = (lft+ rgt)/;
cout<<lft<<" "<<mid<<" "<<rgt<<endl;
if(num[lft]<num[mid]) lft=mid;
else rgt=mid;
mid = rgt;
}
return num[mid];
}
}; int main()
{
vector<int> num{,,,,,};
Solution sol;
cout<<sol.findMin(num)<<endl;
return ;
}
05-06 12:45