当前位置:网站首页>Leetcode153 - find the minimum value in the rotation sort array - array - binary search

Leetcode153 - find the minimum value in the rotation sort array - array - binary search

2022-04-23 14:48:00 Li Fan, hurry up

Original link
 Insert picture description here

Note:

according to nums[0] To determine which of the two paragraphs , To find the midpoint
There's a little problem to pay attention to , That is, the last array is all in ascending order , What we found is the last position , Want to follow nums[0] Compare the one with the smallest output

One more [2, 1] Just walk by yourself If the judgment conditions include = The number is wrong

The code is as follows :

class Solution {
    
public:
    int findMin(vector<int>& nums) {
    
        int l = 0, r = nums.size() - 1;
        while(l < r){
    
            int mid = l + r >> 1;
            if(nums[mid] < nums[0])    r = mid;
            else    l = mid + 1;
        }
        return min(nums[0], nums[r]);
    }
};

版权声明
本文为[Li Fan, hurry up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447599777.html