旋转数组的最小元素

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。

例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。NOTE:给出的所有元素都大于0,若数组大小为0,请返回0

 1 namespace JianZhiOffer
 2 {
 3     class RotateArray
 4     {
 5         public int minNumberInRotateArray(int[] rotateArray)
 6         {
 7             // write code here
 8             if(rotateArray == null || rotateArray.Length == 0)
 9             {
10                 return 0;
11             }
12 
13             int start = 0;
14             int end = rotateArray.Length - 1;
15 
16             while (start < end)
17             {
18                 int mid = start + (end - start) / 2;
19 
20                 // 如果中间的数大于最后一个数,则最小数位于后半部分
21                 if (rotateArray[mid] > rotateArray[end])
22                 {
23                     start = mid + 1;
24                 }
25                 else
26                 {
27                     end = mid;
28                 }
29             }
30             return rotateArray[start];
31         }
32     }
33 }
原文地址:https://www.cnblogs.com/xiaolongren/p/11842431.html