旋转数组的最小数字

题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

思路:

代码:

 1 function minNumberInRotateArray(rotateArray)
 2 {
 3     // write code here
 4     if(rotateArray.length === 0) return 0;
 5     let left = 0,
 6         right = rotateArray.length - 1;
 7     while(left < right){
 8         let mid = left + Math.floor((right-left)/2);
 9         if(rotateArray[mid] > rotateArray[right]){
10             left =mid + 1;
11         }else if(rotateArray[mid] < rotateArray[right]){
12             right = mid;
13         }else{
14             right--;
15         }
16     }
17     return rotateArray[left];
18 }
19 module.exports = {
20     minNumberInRotateArray : minNumberInRotateArray
21 };

 

posted @ 2021-05-24 14:51  icyyyy  阅读(35)  评论(0)    收藏  举报