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) {
        vector<int> res;
        int n = nums.size();
        int i = 0;
        while(i < n){
            // for(int val : nums){
            //     cout<<val<<" ";
            // }
            // cout<<endl;
            if(nums[i] == i + 1 || nums[i] == -1){
                i++;
            }else{
                if(nums[i] == nums[nums[i]-1]){
                    res.push_back(nums[i]);
                    nums[i] = -1;
                    i++;
                }else{
                    swap(nums[i], nums[nums[i]-1]);
                }
                // i++;
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10931988.html

时间: 2024-08-29 18:03:34

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

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

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

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

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

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

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

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

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)

1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单. 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 consta

关于iOS去除数组中重复数据的几种方法

关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方法 可以将NSArray中的元素存入一个字典,然后利用AllKeys或者AllValues取得字典的所有键或值,这些键或值都是去重的.代码: NSArray *dataArray = @[@"2014-04-01",@"2014-04-02",@"2014-04-