【LeetCode】442. Find All Duplicates in an Array 找出数组中所有重复项

题目:

  • Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
    Request: (1) Find all the elements that appear twice in this array. (2)Could you do it without extra space and in O(n) runtime?
  • Example:
    Input: [4,3,2,7,8,2,3,1]
    Output: [2,3]

首先, 该问题的一个重要条件就是 1 ≤ a [ i ] ≤ n ( n = s i z e    o f    a r r a y ) 1 ≤ a[i] ≤ n (n = size \ \ of \ \ array) 1a[i]n(n=size  of  array),不然很难满足题目的第二个要求,时间复杂度为 O ( n ) O(n) O(n), 空间复杂度为 O ( 1 ) O(1) O(1);并且题目中明确说明每一个元素只会出现一次或者两次,当然还有一些不会出现。

解题思路一: 首先来看一种正负替换的方法,这类问题的核心是就是找nums[i]和nums[nums[i] - 1]的关系,我们的做法是,对于每个nums[i],我们将其对应的nums[nums[i] - 1]取相反数,如果其已经是负数了,说明之前存在过,我们将其加入结果result中。
比如 输入为[4,3,2,7,8,2,3,1]

inputiindexnums[index]numsresult
[4, 3, 2, 7, 8, 2, 3, 1]03-7[4, 3, 2, -7, 8, 2, 3, 1][]
[4, 3, 2, -7, 8, 2, 3, 1]12-2[4, 3, -2, -7, 8, 2, 3, 1][]
[4, 3, -2, -7, 8, 2, 3, 1]21-3[4, -3, -2, -7, 8, 2, 3, 1][]
[4, -3, -2, -7, 8, 2, 3, 1]36-3[4, -3, -2, -7, 8, 2, -3, 1][]
[4, -3, -2, -7, 8, 2, -3, 1]47-1[4, -3, -2, -7, 8, 2, -3, -1][]
[4, -3, -2, -7, 8, 2, -3, -1]513[4, 3, -2, -7, 8, 2, -3, -1][2]
[4, 3, -2, -7, 8, 2, -3, -1]622[4, 3, 2, -7, 8, 2, -3, -1][2, 3]
[4, 3, 2, -7, 8, 2, -3, -1]70-4[-4, 3, 2,-7, 8, 2, -3, -1][2, 3]
input:43278231
i01234567
index32167120
nums[index]-7-2-3-3-132-4

最终获得的nums: [-4, 3,2,-7,8,2,-3,-1 ]

// 时间复杂度:O(n)    空间复杂度: O(1)
public List<Integer> findDuplicates(int[] nums) {
	List<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < nums.length; i ++) {
		int index = Math.abs(nums[i]) - 1;
        if (nums[index] < 0) {
			result.add(Math.abs(index + 1));
		}
	    nums[index] = -nums[index];
    }
	return result;
}

解题思路二: 下面的方法是在nums[nums[i]-1]位置累加数组长度n,因此需要注意nums[i]-1有可能越界,所以在取下标时对n取余,最后要找出现两次的数只需要看nums[i]的值是否大于2n即可。

inputiindexnums[index]nums
[4, 3, 2, 7, 8, 2, 3, 1]0315[4, 3, 2, 15, 8, 2, 3, 1]
[4, 3, 2, 15, 8, 2, 3, 1]1210[4, 3, 10, 15, 8, 2, 3, 1]
[4, 3, 10, 15, 8, 2, 3, 1]2111[4, 11, 10, 15, 8, 2, 3, 1]
[4, 11, 10, 15, 8, 2, 3, 1]3611[4, 11, 10, 15, 8, 2, 11, 1]
[4, 11, 10, 15, 8, 2, 11, 1]479[4, 11, 10, 15, 8, 2, 11, 9]
[4, 11, 10, 15, 8, 2, 11, 9]5119[4, 19, 10, 15, 8, 2, 11, 9]
[4, 19, 10, 15, 8, 2, 11, 9]6218[4, 19, 18, 15, 8, , 11, 9]
[4, 19, 18, 15, 8, 2, 11, 9]7012[12, 19, 18, 15, 8, 2, 11, 9]

最后遍历完nums[i]数组为[12, 19, 18, 15, 8, 2, 11, 9],我们发现有两个数字19和18大于2n,那么就可以通过i+1来得到正确的结果2和3了

// 时间复杂度:O(n)    空间复杂度: O(1)
public List<Integer> findDuplicates2(int[] nums) {
	List<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < nums.length; i ++) {
	    // int index =  (nums[i] - 1) % nums.length;
		nums[(nums[i] - 1) % nums.length] += nums.length;   // 将原数组当成hash来使用,出现过的数字对应的下标位置上加上数组长度
	}
    for (int i = 0; i < nums.length; i ++) {
		if (nums[i] > 2 * nums.length)
	        result.add(i + 1);
    }
	return result;
}

两种方法大同小异,都是将元素组本身当作hash来使用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值