数组中重复的数字

题目:

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

输入包括三个参数:数组、数组长度、待求的任意一个重复的数字;
输出:如果有重复的数字,则为真,否则为假。

方法一:

创建一个数组,将其元素初始化为0,用于保存原数组的中每种元素出现的个数,遍历原数组,统计每一种元素出现的个数,当元素个数大于1时,取出该元素,返回真,找不到元素个数大于1的元素,返回假

注:数组一定要初始化为0,否则数组元素的值不确定

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        if(length<2)
            return false;
        int has[255]={0};
        for(int i=0;i<length;i++)
            has[numbers[i]]++;
        for(int i=0;i<length;i++)
        {
            if(has[numbers[i]]>1)
            {
                *duplication=numbers[i];
                return true;
            }
        }
        return false;
    }
};

缺点:不知道创建数组时,不知道创建多大的数组
时间复杂度:O(n),空间复杂度:O(n)

方法二:

现在用关联容器map代替数组,将元素统计进关联容器map中,如果元素个数大于1(m.second>1),则元素值(m.first)就是要找的重复元素

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        if(length<2)
            return false;
        map<int,size_t>result;
        for(int i=0;i<length;i++)
            result[numbers[i]]++;
        for(auto w:result)                //注意循环结构
        {
            if(w.second>1)
            {
                *duplication=w.first;
                return true;
            }
        }
        return false;
    }
};

时间复杂度:O(n),空间复杂度:O(n)

方法三:交换

0~n-1正常的排序应该是A[i]=i;因此可以通过交换的方式,将它们都各自放回属于自己的位置;

从头到尾扫描数组A,当扫描到下标为i的数字m时,首先比较这个数字m是不是等于i,如果是,则继续扫描下一个数字;如果不是,则判断它和A[m]是否相等,如果是,则找到了第一个重复的数字(在下标为i和m的位置都出现了m);如果不是,则把A[i]和A[m]交换,即把m放回属于它的位置;

重复上述过程,直至找到一个重复的数字;
(将每个数字放到属于自己的位置最多交换两次)

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        if(length<2)
            return false;
        for(int i=0;i<length;i++)
        {
            if(numbers[i]<0 && numbers[i]>length-1)
                return false;
        }
        for(int i=0;i<length;i++)
        {
            while(numbers[i]!=i)
            {
                if(numbers[i]==numbers[numbers[i]])
                {
                    *duplication=numbers[i];
                    return true;
                }
                else
                    swap(numbers[i],numbers[numbers[i]]);
            }
        }
        return false;
    }
};

时间复杂度:O(n),空间复杂度:O(1)

方法四:排序

将数组排序,然后扫描排序后的数组即可。

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) 
    {
        if(length<2)
            return false;
        for(int i=0;i<length;i++)
        {
            if(numbers[i]<0 && numbers[i]>length-1)
                return false;
        }
        
        sort(numbers,numbers+length);
        
        for(int i=0;i<length-1;i++)
        {
            if(numbers[i]==numbers[i+1])
            {
                *duplication=numbers[i];
                return true;
            }
        }
        return false;
    }
};

时间复杂度:O(nlogn),空间复杂度:O(1)

方法五:

将每一个元素 c 作为下标访问对应下标的元素 numbers[|c|],如果 numbers[|c|] >= 0,表示元素 c 至今只出现一次,继续遍历,当 numbers[|c|] < 0 时,说明元素 c 已经第二次出现,找到了重复的数字。

class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    //        duplication: (Output) the duplicated number in the array number
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    bool duplicate(int numbers[], int length, int* duplication) {
        for(int i = 0; i < length; i++){
            if (numbers[abs(numbers[i])]>=0)
                numbers[abs(numbers[i])] *= -1;
            else {
                *duplication = abs(numbers[i]);
                return true;
            }
        }
        *duplication = -1;
        return false;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值