Title link: www.acwing.com/problem/con…

Analysis of the

  1. The dichotomous property is whether greater thannums[0]
  2. Be strict with the first paragraph>= nums[0]The second paragraph< nums[0]Need to bewhile(n > 0 && nums[n] == nums[0]) n--;
  3. Binary 3.1if(nums[mid] < nums[0])The second paragraph is the first point in the second paragraphr = mid 3.2 elseThe first paragraphl = mid + 1

Code

/ * * *@param {number[]} nums
 * @return {number}* /
var findMin = function(nums) {
    let n = nums.length - 1;
    if(n < 0) return -1;
    //
    while(n > 0 && nums[n] == nums[0]) n--;
    if(nums[n] >= nums[0]) return nums[0]; // The second paragraph does not exist
    let l = 0, r = n;
    while(l < r){
        let mid = l + r >> 1;
        if(nums[mid] < nums[0]) r = mid;
        else l = mid + 1;
    }
    
    return nums[l];
};
Copy the code