Sword refers to Offer ------- the smallest number of the rotating array

Insert picture description here

Idea: This
question is actually a bit tricky. That is if the array Liman has duplicate values. For example, in the case of 3,1,1,2,2,2,2,2, this question is undoubtedly done with dichotomy. You only need to find the boundary of the last element of the array, but you are afraid of the above. In special cases, the approach here is to move the element at the end of the dividing line one place forward, and use the previous one to find the dividing line. If the previous one has duplicate values, then move forward.

Code:

class Solution {
    
    
public:
    int minArray(vector<int>& numbers) {
    
    
        int i = 0, j = numbers.size() - 1;
        while (i < j) {
    
    
            int m = (i + j) / 2;
            if (numbers[m] > numbers[j]) i = m + 1;
            else if (numbers[m] < numbers[j]) j = m;
            else j--;
        }
        return numbers[i];
    }
};

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/115067229