我是靠谱客的博主 干净星星,最近开发中收集的这篇文章主要介绍剑指offer-求旋转数组的最大最小数值,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

/* 
Q:
    旋转数组最大最小问题。
    把一个数组的最开始若干个元素搬到数组的末尾,我们称之为数组的旋转,
    输入一个递增排序的数组的旋转数组,输出旋转数组的最小(最大)元素,
    例如:
        输入:{3,4,5,1,2}
        输出:1
*/

#include <iostream>
#include <cstdio>

int minInOrder(int arr[], int index_1, int index_2)
{
    int result = *(arr + index_1);
    for(int i = index_1 + 1; i <= index_2; ++i)
    {
        if(*(arr + i) < result)
            result = *(arr + i);
    }
    return result;
}

int maxInOrder(int arr[], int index_1, int index_2)
{
    int result = *(arr + index_1);
    for(int i = index_1 + 1; i <= index_2; ++i)
    {
        if(*(arr + i) > result)
            result = *(arr + i);
    }
    return result;
}

int rotatedMin(int arr[], int length)
{
    if(arr == nullptr || length <= 0)
    {
        std::cout << "arrar error!" << std::endl;
        exit(1);
    }

    int index_1 = 0;
    int index_2 = length - 1;
    int midIndex = index_1;
    while(*(arr + index_1) >= *(arr + index_2))
    {
        if(index_2 - index_1 == 1)
        {
            midIndex = index_2;
            break;
        }
        midIndex = (index_1 + index_2) / 2;
        if(*(arr + midIndex) == *(arr + index_1) && *(arr + midIndex) == *(arr + index_2))
            return minInOrder(arr, index_1, index_2);
        if(*(arr + midIndex) >= *(arr + index_1))
            index_1 = midIndex;
        if(*(arr + midIndex) <= *(arr + index_2))
            index_2 = midIndex;
    }
    return *(arr + midIndex);
}

int rotatedMax(int arr[], int length)
{
    if(arr == nullptr || length <= 0)
    {
        std::cout << "arrar error!" << std::endl;
        exit(1);
    }

    int index_1 = 0;
    int index_2 = length - 1;
    int midIndex = index_2;
    while(*(arr + index_1) >= *(arr + index_2))
    {
        if(index_2 - index_1 == 1)
        {
            midIndex = index_1;
            break;
        }
        midIndex = (index_1 + index_2) / 2;
        if(*(arr + midIndex) == *(arr + index_1) && *(arr + midIndex) == *(arr + index_2))
            return maxInOrder(arr, index_1, index_2);
        if(*(arr + midIndex) >= *(arr + index_1))
            index_1 = midIndex;
        if(*(arr + midIndex) <= *(arr + index_2))
            index_2 = midIndex;
    }
    return *(arr + midIndex);
}

void test_rotatedMin()
{
    int arr[] = {3,4,5,1,2};
    int result = rotatedMin(arr, 5);
    std::cout << "found min value = " << result << std::endl;
    result = rotatedMax(arr, 5);
    std::cout << "found max value = " << result << std::endl;

    int arr_2[] = {1,0,1,1,1};
    result = rotatedMin(arr_2, 5);
    std::cout << "found min value = " << result << std::endl;
    result = rotatedMax(arr_2, 5);
    std::cout << "found max value = " << result << std::endl;

    int arr_3[] = {1,1,1,0,1};
    result = rotatedMin(arr_3, 5);
    std::cout << "found min value = " << result << std::endl;
    result = rotatedMax(arr_3, 5);
    std::cout << "found max value = " << result << std::endl;

    int arr_4[] = {3, 6, 6, 9, 0};
    result = rotatedMin(arr_4, 5);
    std::cout << "found min value = " << result << std::endl;
    result = rotatedMax(arr_4, 5);
    std::cout << "found max value = " << result << std::endl;

    int arr_5[] = {3, 6, 6, 9, 10};
    result = rotatedMin(arr_5, 5);
    std::cout << "found min value = " << result << std::endl;
    result = rotatedMax(arr_5, 5);
    std::cout << "found max value = " << result << std::endl;
}

int main(int argc, char**argv)
{

    test_rotatedMin();

    return 0;
}
 

最后

以上就是干净星星为你收集整理的剑指offer-求旋转数组的最大最小数值的全部内容,希望文章能够帮你解决剑指offer-求旋转数组的最大最小数值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部