剑指Offer-3:数组中重复的数字

题目1:找出数组中的重复数字

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。

Example:

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

问题解析:

  • 元素在0到n-1的范围;
  • 给出任意一个重复数字。

链接:

剑指Offer(第2版):P39

思路标签:

算法:排序hash

数据结构:数组

解法的逐步优化:排序寻找重复——Hash表存储——数组索引的技巧性应用

解答:

1. C++
  • (1) 一个简单的思路是先将数组排序,然后从头开始寻找重复数字。排序的时间复杂度为O(nlogn);
  • (2) 利用hash表存储元素,若表中存在元素则找到重复数字。Hash查询时间仅用O(1),算法时间复杂度为O(n),但是需要一个哈希表,空间复杂度为O(n);
  • (3) 利用元素数字都在0到n-1的范围内的特点,若不存在重复数字,则排序后的数字就在与其相同的索引值的位置,即数字i在第i个位置。遍历的过程中寻找位置和元素不相同的值,并进行交换排序。时间复杂度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 (numbers == nullptr || length <= 0){
            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;
                }

                //交换
                int temp = numbers[i];
                numbers[i] = numbers[temp];
                numbers[temp] = temp;
            }
        }

        return false;
    }
};

C++相关:

C++ 11中使用nullptr代表空指针:


题目2:不修改数组找出重复数字

在一个长度为n+1的数组里所有数字都在1~n的范围内,所有数组中至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的数组。

Example:

如果输入长度为8的数组{2,3,5,4,3,2,6,7},那么对应的输出是重复数字2或者3。

问题解析:

  • 数组长度为n,元素在1到n的范围;
  • 必定至少有一个数字重复。

链接:

剑指Offer(第2版):P41

思路标签:

算法:二分查找

数据结构:数组

空间换时间:辅助数组,时间复杂度O(n),空间复杂度O(n);
时间换空间:二分查找,时间复杂度O(nlogn),空间复杂度O(1)。

解答:

1. C++
  • (1) 第一种是利用辅助数组来将原数组的对应元素存储到对应的位置,需要额外的O(n)的空间;
  • (2) 第二种是利用二分查找的方式。
    • 我们将1~n从中间分成两份,1~m和m+1~n,遍历数组分别统计数组中出现1~m的元素的个数和出现m+1~n的个数,对于统计个数大于对应的长度时,则说明该份中出现了重复元素;
    • 对出现重复元素的一份继续进行折半划分,一直到单份长度为1,但统计却含有多个元素的情况,则找到重复元素;
    • 这种方法无法保证找出所有的重复数字,需要在解决前了解需求。
class Solution {
public:
    // Parameters:
    //        numbers:     an array of integers
    //        length:      the length of array numbers
    // Return value:       one of the duplicate numbers

    int getDuplication(const int* numbers, int length) {
        // 空指针和空数据的情况
        if (numbers == nullptr || length <= 0){
            return -1;
        }

        int start = 1;
        int end = length-1;
        while (end >= start){
            int middle = ((end - start)>>1) + start; // (end-start)/2 +start

            int count = countRange(numbers, length, start, middle);
            if (end == start){
                if (count > 1) return start;
                else break;
            }

            if (count > (middle-start+1)) end = middle;
            else start = middle + 1;
        }

        return -1;
    }

    int countRange(const int* numbers, int length, int start, int end){
        if (numbers == nullptr) return 0;

        int count = 0;
        for (int i = 0; i < length; ++i){
            if (numbers[i] >= start && numbers[i] <= end)
                ++count;
        }

        return count;
    }
};

算法相关:

二分查找:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值