zoukankan      html  css  js  c++  java
  • 旋转数组的最小元素

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

    例如数组{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 }
  • 相关阅读:
    Alpha项目冲刺_博客链接合集
    项目系统设计
    项目需求分析
    项目选题
    项目展示
    原型设计 + 用户规格说明书
    测试与优化
    结对作业1
    MathExam6317
    js入门基础
  • 原文地址:https://www.cnblogs.com/xiaolongren/p/11842431.html
Copyright © 2011-2022 走看看