zoukankan      html  css  js  c++  java
  • 剑指offer---旋转数组的最小数字

    题目:旋转数组的最小数字

    要求:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

    1 class Solution {
    2 public:
    3     int minNumberInRotateArray(vector<int> rotateArray) {
    4         
    5     }
    6 };

    解题代码:

     1 class Solution {
     2 public:
     3     int minNumberInRotateArray(vector<int> rotateArray) {
     4         if(rotateArray.size() == 0)
     5             return 0;
     6         int left = 0;
     7         int right = rotateArray.size()-1;
     8         int pMid = (left + right) / 2;
     9         if(rotateArray[left] < rotateArray[right])
    10             return rotateArray[0];
    11         else{
    12             while(right-left != 1){
    13                 if(rotateArray[left] == rotateArray[right] && rotateArray[left] == rotateArray[pMid])
    14                     return MinInOrder(rotateArray, left, right);
    15                 else{
    16                     if(rotateArray[pMid] >= rotateArray[left]){
    17                         left = pMid;
    18                     }
    19                     else{
    20                         right = pMid;
    21                     }
    22                     pMid = (left + right) / 2;
    23                 }
    24             }
    25             return rotateArray[right];
    26         }
    27     }
    28 
    29 private:
    30     int MinInOrder(vector<int> array, int left, int right){
    31         int res = array[left];
    32         for(int i=left+1; i<=right; i++){
    33             if(array[i] < res)
    34                 res = array[i];
    35         }
    36         return res;
    37     }
    38 };
  • 相关阅读:
    Loadrunner日志设置与查看
    Mysqlfunc.c
    loadrunner生成随机uuid的方法
    数据库连接
    FAQ_2
    JAVA VUser
    FAQ_1
    LoadRunner中的Web 函数列表
    MySQL性能诊断与调优
    LoadRunner书籍推荐
  • 原文地址:https://www.cnblogs.com/iwangzhengchao/p/9847826.html
Copyright © 2011-2022 走看看