欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-10 20:36:20
...

题目:

  • 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]

首先, 该问题的一个重要条件就是1a[i]n(n=size  of  array)1 ≤ a[i] ≤ n (n = size \ \ of \ \ array),不然很难满足题目的第二个要求,时间复杂度为O(n)O(n), 空间复杂度为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]

input i index nums[index] nums result
[4, 3, 2, 7, 8, 2, 3, 1] 0 3 -7 [4, 3, 2, -7, 8, 2, 3, 1] []
[4, 3, 2, -7, 8, 2, 3, 1] 1 2 -2 [4, 3, -2, -7, 8, 2, 3, 1] []
[4, 3, -2, -7, 8, 2, 3, 1] 2 1 -3 [4, -3, -2, -7, 8, 2, 3, 1] []
[4, -3, -2, -7, 8, 2, 3, 1] 3 6 -3 [4, -3, -2, -7, 8, 2, -3, 1] []
[4, -3, -2, -7, 8, 2, -3, 1] 4 7 -1 [4, -3, -2, -7, 8, 2, -3, -1] []
[4, -3, -2, -7, 8, 2, -3, -1] 5 1 3 [4, 3, -2, -7, 8, 2, -3, -1] [2]
[4, 3, -2, -7, 8, 2, -3, -1] 6 2 2 [4, 3, 2, -7, 8, 2, -3, -1] [2, 3]
[4, 3, 2, -7, 8, 2, -3, -1] 7 0 -4 [-4, 3, 2,-7, 8, 2, -3, -1] [2, 3]
input: 4 3 2 7 8 2 3 1
i 0 1 2 3 4 5 6 7
index 3 2 1 6 7 1 2 0
nums[index] -7 -2 -3 -3 -1 3 2 -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即可。

input i index nums[index] nums
[4, 3, 2, 7, 8, 2, 3, 1] 0 3 15 [4, 3, 2, 15, 8, 2, 3, 1]
[4, 3, 2, 15, 8, 2, 3, 1] 1 2 10 [4, 3, 10, 15, 8, 2, 3, 1]
[4, 3, 10, 15, 8, 2, 3, 1] 2 1 11 [4, 11, 10, 15, 8, 2, 3, 1]
[4, 11, 10, 15, 8, 2, 3, 1] 3 6 11 [4, 11, 10, 15, 8, 2, 11, 1]
[4, 11, 10, 15, 8, 2, 11, 1] 4 7 9 [4, 11, 10, 15, 8, 2, 11, 9]
[4, 11, 10, 15, 8, 2, 11, 9] 5 1 19 [4, 19, 10, 15, 8, 2, 11, 9]
[4, 19, 10, 15, 8, 2, 11, 9] 6 2 18 [4, 19, 18, 15, 8, , 11, 9]
[4, 19, 18, 15, 8, 2, 11, 9] 7 0 12 [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来使用

相关标签: Array