【C/C++】旋转数组的最小数字/ 剑指offer

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        int n = rotateArray.size();
        if (n == 0) return 0;
        int index1 = 0, index2 = n-1, mid = index1;
        while(index1 < index2)
        {
            if (index2 - index1 == 1)
            {
                return rotateArray[index2];
            } 

            // mid = (index1 + index2)/2; 
            mid = index1 + (index2 - index1)/2;

            // 在1中
            if (rotateArray[mid] >= rotateArray[index1])
            {
                index1 = mid;
//如果这个地方index1 = mid + 1,最后return rotateArray[index1]
            }
            // 在2中
            else if (rotateArray[mid] <= rotateArray[index2])
            {
                index2 = mid;
            }

            else if (rotateArray[index1] == rotateArray[index2] && rotateArray[index1] == rotateArray[mid])
            {
                int min = 0x3F3F3F3F;
                for (int i = index1; i<=index2; ++i)
                {
                    if (rotateArray[i] < min)
                    {
                        min = rotateArray[i];
                    }
                }
                return min;
            }
        }
        return (rotateArray[mid]);
// return rotateArray[index1]
    }

};

int main()
{
    vector <int> rotateArray = {6501,6828,6963,7036,7422,7674,8146,8468,8704,8717,9170,9359,9719,9895,9896,9913,9962,154,293,334,492,1323,1479,1539,1727,1870,1943,2383,2392,2996,3282,3812,3903,4465,4605,4665,4772,4828,5142,5437,5448,5668,5706,5725,6300,6335};
    Solution solution;
    cout << solution.minNumberInRotateArray(rotateArray);
    system("pause");
}
原文地址:https://www.cnblogs.com/kinologic/p/14708063.html