LeetCode-442. Find All Duplicates in an Array

LeetCode-442. Find All Duplicates in an ArrayLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/find-all-duplicates-in-an-array/

题目描述

Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.

You must write an algorithm that runs in O(n) time and uses only constant extra space.

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]

Example 2:

Input: nums = [1,1,2]
Output: [1]

Example 3:

Input: nums = [1]
Output: []

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= n
  • Each element in nums appears once or twice.

解题思路

【C++解法】

1、负负得正

class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        // negate all values
        for (int i=0; i<nums.size(); ++i) {nums[i] = -nums[i];}
        // make all visitable index positive      
        for (int i=0; i<nums.size(); ++i) {nums[abs(nums[i])-1] = abs(nums[abs(nums[i])-1]);}
        // if index is reachable negate it => two time repeated will get positive and missing will remain negative(from first pass)
        for (int i=0; i<nums.size(); ++i) {nums[abs(nums[i])-1] = -nums[abs(nums[i])-1];}
        // all positive index left will be solution
        vector<int>sol;
        for (int i=0; i<nums.size(); ++i) {if (nums[i]>0) {sol.push_back(i+1);}}
        return sol;
    }
};

2、交换排序

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

3、哈希标记

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

【Java解法】

1、负负得正

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

2、交换排序

class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        int i = 0, n = nums.length;
        while(i < n){
            if(nums[i] != nums[nums[i]-1]) {nums = swap(nums, i, nums[i]-1);}
            else i++;
        }
        for(i=0; i<n; i++) {if(nums[i] != i+1) res.add(nums[i]);}
        return res;
    }
    
    public static int[] swap(int[] nums, int a,int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
        return nums;
    }
}

3、哈希标记

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

参考文献

【1】Java中List集合的常用方法 - xiaostudy - 博客园

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值