哈希表题目:数组中重复的数据

题目

标题和出处

标题:数组中重复的数据

出处:442. 数组中重复的数据

难度

6 级

题目描述

要求

给你一个长度为 n \texttt{n} n 的整数数组 nums \texttt{nums} nums,其中 nums \texttt{nums} nums 的所有整数都在范围 [1,   n] \texttt{[1, n]} [1, n] 内,且每个整数出现一次两次。请你找出所有出现两次的整数,并以数组形式返回。

你必须设计并实现一个时间复杂度为 O(n) \texttt{O(n)} O(n) 且仅使用常量额外空间的算法解决此问题。

示例

示例 1:

输入: nums   =   [4,3,2,7,8,2,3,1] \texttt{nums = [4,3,2,7,8,2,3,1]} nums = [4,3,2,7,8,2,3,1]
输出: [2,3] \texttt{[2,3]} [2,3]

示例 2:

输入: nums   =   [1,1,2] \texttt{nums = [1,1,2]} nums = [1,1,2]
输出: [1] \texttt{[1]} [1]

示例 3:

输入: nums   =   [1] \texttt{nums = [1]} nums = [1]
输出: [] \texttt{[]} []

数据范围

  • n = nums.length \texttt{n} = \texttt{nums.length} n=nums.length
  • 1 ≤ n ≤ 10 5 \texttt{1} \le \texttt{n} \le \texttt{10}^\texttt{5} 1n105
  • 1 ≤ nums[i] ≤ n \texttt{1} \le \texttt{nums[i]} \le \texttt{n} 1nums[i]n
  • nums \texttt{nums} nums 中的每个元素出现一次两次

解法

思路和算法

这道题要求找出数组 nums \textit{nums} nums 中的所有出现两次的整数并返回。常规的做法是使用哈希表存储数组中的整数,遍历数组并将每个整数存入哈希表,如果遍历到一个元素时发现该元素已经在哈希表中,则该元素是出现两次的整数。对于长度为 n n n 的数组,使用哈希表的时间复杂度是 O ( n ) O(n) O(n),符合题目要求,但是空间复杂度是 O ( n ) O(n) O(n),不符合题目要求的常数空间。

为了将空间复杂度降低到常数,不能额外创建哈希表,只能原地修改数组。由于数组 nums \textit{nums} nums 的长度是 n n n,下标范围是 [ 0 , n − 1 ] [0, n - 1] [0,n1],每个元素都在范围 [ 1 , n ] [1, n] [1,n] 内,因此可以将数组看成哈希表,利用数组下标的信息表示每个整数是否出现两次。对于下标 index \textit{index} index,满足 0 ≤ index < n 0 \le \textit{index} < n 0index<n 1 ≤ index + 1 ≤ n 1 \le \textit{index} + 1 \le n 1index+1n nums [ index ] \textit{nums}[\textit{index}] nums[index] 可以用于表示整数 index + 1 \textit{index} + 1 index+1 是否出现两次。

遍历数组 nums \textit{nums} nums。对于元素 num \textit{num} num,其对应的下标为 index = ∣ num ∣ − 1 \textit{index} = |\textit{num}| - 1 index=num1,根据 nums [ index ] \textit{nums}[\textit{index}] nums[index] 的正负性执行如下操作:

  • 如果 nums [ index ] > 0 \textit{nums}[\textit{index}] > 0 nums[index]>0,则将 nums [ index ] \textit{nums}[\textit{index}] nums[index] 的值更新为其相反数;

  • 如果 nums [ index ] < 0 \textit{nums}[\textit{index}] < 0 nums[index]<0,则 ∣ nums ∣ = index + 1 |\textit{nums}| = \textit{index} + 1 nums=index+1 是出现两次的整数,将其添加到结果中。

上述做法的原理如下:

  1. 初始时数组 nums \textit{nums} nums 中的整数都是正数,表示尚未被访问;

  2. 当一个整数被访问时,如果该整数对应的下标处的元素是正数,则该整数尚未被访问,因此将该整数对应的下标处的元素改成其相反数,相反数是负数,表示被访问了一次;

  3. 当一个整数被访问时,如果该整数对应的下标处的元素是负数,则该整数已经被访问,因此该整数被第二次访问,即该整数是出现两次的整数。

需要注意的是,遍历数组 nums \textit{nums} nums 的过程中,遍历到的元素 num \textit{num} num 可能已经被改成负数,因此在计算下标 index \textit{index} index 时需要对 num \textit{num} num 取绝对值然后减 1 1 1

代码

class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> duplicates = new ArrayList<Integer>();
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int num = nums[i];
            int index = Math.abs(num) - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
            } else {
                duplicates.add(index + 1);
            }
        }
        return duplicates;
    }
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是数组 nums \textit{nums} nums 的长度。需要遍历数组 nums \textit{nums} nums 一次,对于每个元素的操作时间都是 O ( 1 ) O(1) O(1)

  • 空间复杂度: O ( 1 ) O(1) O(1)。由于是原地修改数组,因此额外使用的空间是常数。注意返回值不计入空间复杂度。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大的车尔尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值