Title link: www.acwing.com/problem/con…
Analysis of the
- The dichotomous property is whether greater than
nums[0]
- Be strict with the first paragraph
>= nums[0]
The second paragraph< nums[0]
Need to bewhile(n > 0 && nums[n] == nums[0]) n--;
- Binary 3.1
if(nums[mid] < nums[0])
The second paragraph is the first point in the second paragraphr = mid
3.2else
The 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