运用二分法思想
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
你可以假设数组中不存在重复的元素。
class Solution {
public:
/**
* @param num: a rotated sorted array
* @return: the minimum number in the array
*/
int findMin(vector<int> &num) {
// write your code here
int low = 0;
int high = num.size()-1;
int mid = (low+high)>>1;
if(num[low] <num [high]) {
return num[low];
}
while (low+1 < high) {
mid = (low+high)>>1;
if (num[low] > num[mid]) {
high = mid;
}
else {
low =mid;
}
}
return num[high];
}
};