栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【剑指Offer | C++ 】面试题3:数组中重复的数字

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【剑指Offer | C++ 】面试题3:数组中重复的数字

题目一、找出数组中重复的数字

在一个长度为n的数组里的所有数字都在0~n-1的范围内。

数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了多少次。请找出数组中任意一个重复的数字。

例如,如果长度为7 的数组{2,3,1,0,5,3},那么对应的输出是重复的数字2或3。

该题还可用哈希表或者排序解决

关键点:

  • 异常处理
  • const规范

代码:

#include 
using namespace std;


bool duplicate(const int nums[], const int length, int& duplication) {
    //异常处理
    if (nums == nullptr || length <= 0) {
        return false;
    }
    for (int i = 0; i < length; i++) {
        if (nums[i]<0 || nums[i]>length - 1)return false;
        while (nums[i]!=i)
        {   
            if (nums[i] == nums[nums[i]]) {
                duplication = nums[i];
                return true;
            }
            int temp = nums[i];
            nums[i] = nums[nums[i]];
            nums[temp] = temp;
        }
    }
    return false;

}

int main()
{
    int a[] = { 0,1,3,3 };
    int dup = -1;
    cout << duplicate(a, 4, dup) << dup;
}

题目二、不修改数组找出重复的数字

比起上面的题目添加一个要求

不修改数组!

可以使用辅助数组判断
时间复杂度:O(n)
空间复杂度:O(n)

以时间换空间的算法:

#include 
using namespace std;


bool duplicate(int nums[], int length, int& duplication) {
    //异常处理
    if (nums == nullptr || length <= 0) {
        return false;
    }
    int start = 1;
    int end = length - 1;
    for (int i = 0; i < length; i++) {

        int mid = ((end - start) >> 1) + start; // (start+end)/2 防溢出
        int count = 0;
        for (int j = 0; j < length; j++) 
            if (nums[j] >= start && nums[j] <= mid) count++;
                
        if (start == end)
        {
            if (count > 1) {
                duplication = nums[start];
                return true;
            }
            else return false;
                
        }
        if (count<=mid-start+1) start = mid + 1;
        else end = mid;
    }

    return false;

}

int main()
{
    int a[] = { 0,1,3,3 };
    int dup = -1;
    cout << duplicate(a, 4, dup) << dup;
}

每次都查找前半段
如果次数多就在前面,否则在后面

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954402.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号