前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode笔记:442. Find All Duplicates in an Array

LeetCode笔记:442. Find All Duplicates in an Array

作者头像
Cloudox
发布2021-11-23 15:59:09
2360
发布2021-11-23 15:59:09
举报
文章被收录于专栏:月亮与二进制月亮与二进制

问题:

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,8,2,3,1] Output: [2,3]

大意:

给出一个整型数组, 1 ≤ a[i] ≤ n (n为数组的尺寸),一些元素出现了两次,另一些只出现一次。 找到所有数组中出现了两次的元素。 你能不能不使用额外的空间,在O(n)时间内完成? 例子: 输入: [4,3,2,7,8,2,3,1] 输出: [2,3]

思路:

题目说明了数组中元素的范围,那么可以依此创建一个长度为n的新整型数组,其每个位置的值大小表示对应数字出现的次数,遍历原数组,遇到那个数字就将新数组对应值的位置的元素值加一,就记录下每个数字出现的次数了,之后找出出现次数为2的添加到结果List中即可。

这个做法使用了额外的O(n)的空间,并不符合要求。

代码(Java):

代码语言:javascript
复制
public class Solution {
    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> result = new ArrayList<Integer>();
        int[] count = new int[nums.length+1];
        for (int i = 0; i < nums.length; i++) {
            count[nums[i]]++;
        }
        for (int i = 1; i < count.length; i++) {
            if (count[i] == 2) result.add(i);
        }
        return result;
    }
}

他山之石:

代码语言:javascript
复制
public class Solution {
    // 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;
    }
}

这个做法在注释中也解释了,遍历数组,没遇到一个元素,将其值对应的位置上的那个元素取负数,当然因为元素的值是从1开始的,所以变成位置的时候都要减一,每次换成位置时都要用绝对值来算,因为出现了两次的元素,在之前就已经被变成负数了,所以借此可以判断,如果该位置的元素是个负数,说明之前出现过一次,就记录下来。

这个做法就没有用额外的空间,时间复杂度也是O(n)。

合集:https://github.com/Cloudox/LeetCode-Record

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/11/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题:
  • 大意:
  • 思路:
  • 代码(Java):
  • 他山之石:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档