[LeetCode] 154. Find Minimum in Rotated Sorted Array II

Find the minimum of the two rotation orderly array. Thinking with [LeetCode] 153. Find Minimum in Rotated Sorted Array is very close, the only condition is more input if there are duplicate numbers how to do. It should be more than a condition judgment, if the judgment is not out of the relationship between the mid and the end of time, can only try end-- or start ++. So this problem will be the worst case time O (n).

Time O (logn), worst case O (n)

Space O (1)

 1 /**
 2  * @param {number[]} nums
 3  * @return {number}
 4  */
 5 var findMin = function(nums) {
 6     // corner case
 7     if (nums === null || nums.length === 0) return -1;
 8 
 9     // normal case
10     let start = 0;
11     let end = nums.length - 1;
12     while (start + 1 < end) {
13         let mid = Math.floor(start + (end - start) / 2);
14         if (nums[mid] < nums[end]) {
15             end = mid;
16         } else if (nums[mid] > nums[end]) {
17             start = mid + 1;
18         } else {
19             end--;
20         }
21     }
22     if (nums[start] < nums[end]) return nums[start];
23     else return nums[end];
24 };

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/11791616.html