编程题——查找和排序

本文介绍了一道编程题,涉及查找旋转数组的最小数字。通过分析非减排序数组的旋转特性,利用二分法可以高效地找到最小值。在遇到特殊情况如三个指针指向相同值时,需要调整策略,对部分数组进行顺序遍历以找到最小数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  •  旋转数组的最小数字

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

解题代码:
 

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        //排除不可能
        if(rotateArray.empty()){
            return 0;
        }
        
        int leftIndex = 0;
        int rightIndex = rotateArray.size() - 1;
        int midIndex = leftIndex;
        
        while(rotateArray[leftIndex] >= rotateArray[rightIndex]){
            midIndex = (leftIndex + rightIndex) / 2;
            
            //如果左右指针挨在一起了
            if((rightIndex - leftIndex) == 1){
                break;
            }
            
            //如果leftIndex, rightIndex, midIndex对应的数组值都是相同的则只能进入顺序查找
            if((rotateArray[leftIndex] == rotateArray[rightIndex])
               &&(rotateArray[leftIndex] == rotateArray[midIndex])){
                return minOrder(rotateArray, leftIndex, rightIndex);
            }
            
            //二分法
            if(rotateArray[midIndex] >= rotateArray[leftIndex]){
                leftIndex = midIndex;
            }else if(rotateArray[midIndex <= rotateArray[rightIndex]]){
                rightIndex = midIndex;
            }
            
        }
        return rotateArray[rightIndex];
        
    }
private:
    int minOrder(vector<int> rotateArray, int leftIndex, int rightIndex){
        int result = rotateArray[leftIndex];
        for(int i = leftIndex + 1; i <= rightIndex; i++){
            if(result > rotateArray[i]){
                result = rotateArray[i];
                break;
            }
        }
        return result;
    }
};

解题思路:

  1. 简单的说,这道题就是找数组里最小的数
  2. 有很多种做法可以找数组里最小的数,比如遍历,但是如果用了遍历,那你就注定是拿不到欧肥儿的仔了。
  3. 遍历不如二分法查找优秀,二分法时间复杂度只有O(logn)。
  4. 由于题目给出线索,这个数组是其实是一个有序数组拆成的两个有序数组[小~大 小~大],所以这样二分法可以更简便得查找到最小的数。
  5. 但是单用二分法,如果遇到一个情况就GG了。那就是如果,左指针,右指针,中间指针指的数都相等的话,二分法是没有办法找到最小数的。比如[1, 0, 1, 1, 1]。所以如果遇到这种这种情况,我们只能把数组从左指针到右指针的这段数组截下来,顺序遍历得到最小数。

  •  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值