(Java) 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.

Find all the elements that appear twice in this array.

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]

  



一看到寻找重复,就先想到哈希表。O(n)时间复杂度很简单,只要遍历的时候放入哈希表,再出现的就是结果。但这样不满足空间复杂度的要求。一般来说时间和空间都是需要trade off的。保持时间不变减少空间,那么一定有其他的玄机在里面。这道题就是这样,看限定条件1 ≤ a[i] ≤ n ,就知道a[i]-1的范围是0~n,那么它其实可以用作数组的下标。也就是说,数组元素和数组下标其实是对应的关系(看起来像废话…)。每次访问一个元素,同时也是访问一个下标,那么再次访问这个元素的时候就会再次访问对应的下标。这样就可以在其对应的下标指向的元素上面做文章。有什么办法可以让元素既可以起到标记的作用,又不影响其可以继续做下标的属性呢?那就是取负数。这样如果将元素对应下标指向的元素取负数,就知道这个元素已经出现过。而取负数并不影响它可以做下标的属性,因为再取绝对值就好了。所以本质是不是标记遍历到的元素本身,而是用负号标记遍历到的元素对应下标指向的元素。或者可以把本身看成一个哈希表,计算hash的过程同时也是标记的过程。



Java

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

原文地址:https://www.cnblogs.com/tengdai/p/9242731.html

时间: 2024-07-30 13:13:16

(Java) LeetCode 442. Find All Duplicates in an Array —— 数组中重复的数据的相关文章

442 Find All Duplicates in an Array 数组中重复的数据

给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次.找到所有出现两次的元素.你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?示例:输入:[4,3,2,7,8,2,3,1]输出:[2,3]详见:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ C++: 方法一: class Solution { public: vector

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

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. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode 442. Find All Duplicates in an Array

转载请注明出处:http://www.cnblogs.com/liangyongrui/p/6353922.html 开个数组 hash的办法 大家都会. 但是这题不能用辅助空间,所以,我给这个方法起名叫别样hash 因为,所有的数字 都在[1,n] 所以可以用数字的正负来表示hash值 具体见代码. public List<Integer> findDuplicates(int[] nums) { List<Integer> res = new ArrayList<>

leetcode 442. 数组中重复的数据 java

题目: 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? 示例: 输入: [4,3,2,7,8,2,3,1] 输出: [2,3] 解题: class Solution { public List<Integer> findDuplicates(int[] nums) { List<Integer> res = new Array

leetcode 442. 数组中重复的数据(Find All Duplicates in an Array)

目录 题目描述: 示例: 解法: 题目描述: 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? 示例: 输入: [4,3,2,7,8,2,3,1] 输出: [2,3] 解法: class Solution { public: vector<int> findDuplicates(vector<int>& nums) {

LeetCode 442.数组中重复的数据 - JavaScript

题目描述:给定一个整数数组 a,其中 1 ≤ a[i] ≤ n (n 为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在 O(n)时间复杂度内解决这个问题吗? 题目分析 这里的不使用任何额外空间,指的是不为哈希表开辟额外空间.最后要返回的元素,还是要放在数组内的. 解法 :原地哈希 因为不能使用额外空间存储哈希表,所以只能对数组本身做操作.题目提到元素的范围是 1 到 n,并且元素只可能出现 1 次或者 2 次. 因此这里可以使用符号

[Swift]LeetCode442. 数组中重复的数据 | 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. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

442. Find All Duplicates in an Array - LeetCode

Question 442.?Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Java实现: public List<Integer> findDuplicates(int[] nums) { List<Integer> ans = new ArrayList<>(); if (nums.length == 0) return ans; Arrays.so