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]走一遍并一路做好标记,如果成环,则说明有重复值,但这样每次走一遍,都需要先清除标记在做好标记,就很繁琐了。另一方面时间复杂度也比较大了。

  题目代码

public class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> list=new ArrayList<Integer>();
        if(nums==null || nums.length==0 || nums.length==1) return list;
        int index=0,temp,t;
        while(index<nums.length){
            temp=nums[index]-1;
            if(nums[temp]!=temp+1){  //正确的位置上未放有正确的值                t=nums[temp];
                nums[temp]=nums[index];
                nums[index]=t;
            }else
                index++;
        }
        index=0;
        while(index<nums.length){
            if(nums[index]!=index+1) list.add(nums[index]);
            index++;
        }
        return list;
    }
}

  

时间: 2024-10-10 23:59:12

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

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

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

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

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor