链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/
本题是查找问题,可以考虑用二分查找来做,时间复杂度为O(log(n))
class Solution {
public:
int minArray(vector<int>& numbers) {
int low = 0;
int high = numbers.size() - 1;
// 这里加不加等于号都可以
while (low <= high) {
int pivot = low + (high - low) / 2;
if (numbers[pivot] < numbers[high]) {
high = pivot;
}
else if (numbers[pivot] > numbers[high]) {
// 这里+1实际上是因为numbers[pivot]已经不可能是最小的,所以加1查找
low = pivot + 1;
}
else {
high -= 1;
}
}
return numbers[low];
}
};