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<int> findDuplicates(vector<int>& nums) {
        vector<int> res;
        for(int i=0;i<nums.size();++i)
        {
            int idx=abs(nums[i])-1;
            if(nums[idx]<0)
            {
                res.push_back(idx+1);
            }
            nums[idx]=-nums[idx];
        }
        return res;
    }
};

方法二:

class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums)
    {
        vector<int> res;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] != nums[nums[i] - 1])
            {
                swap(nums[i], nums[nums[i] - 1]);
                --i;
            }
        }
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] != i + 1)
            {
                res.push_back(nums[i]);
            }
        }
        return res;
    }
};

参考:https://www.cnblogs.com/grandyang/p/6209746.html

原文地址:https://www.cnblogs.com/xidian2014/p/8900510.html

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

442 Find All Duplicates in an Array 数组中重复的数据的相关文章

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

目录 题目描述: 示例: 解法: 题目描述: 给定一个整数数组 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) {

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

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.数组中重复的数据 - JavaScript

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

JS删除数组中重复的数据

Array.delRepeat = function (arr) {   var _arr = arr.slice(0),retArr = [];   for (var i = 0, len = _arr.length; i < len; i++) {     for (var j = i + 1; j < len; j++) {       if (_arr[i] == _arr[j]) {         break;       }     }     if (j == len) {  

442. 找出数组中重复的元素 Find All Duplicates in an ArrayGiven an array of integers

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

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

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