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.sort(nums);
    int last = nums[0];
    for (int i=1; i<nums.length; i++) {
        if (nums[i] == last) {
            ans.add(last);
        }
        last = nums[i];
    }
    return ans;
}

Ref

// when find a number i, flip the number at position i-1 to negative.
// if the number at position i-1 is already negative, i is the number that occurs twice.

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

原文地址:https://www.cnblogs.com/okokabcd/p/9398476.html

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

442. Find All Duplicates in an Array - LeetCode的相关文章

442. Find All Duplicates in an Array

   题目描述     442. Find All Duplicates in an Array   题目分析 遍历数组,将每次取出的值放入到正确的位置上,如nums[0]的值为4,则将4放入到nums[3]中(交换值方式). 如果正确的位置已经有正确的值(nums[index]==index+1),则不用处理. 如此,将不断地将值放入到正确的位置.而放不进去的值则为重复的值. 补充:可以使用LeetCode 448的思路,遍历数组,每取一值,index=nums[index]走一遍并一路做好标

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

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)

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,

(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,

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

Remove Duplicates From Sorted Array leetcode java

算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array

Remove Duplicates from Sorted Array -- leetcode

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = 

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