leetcode 442. Find All Duplicates in an Array(找出重复元素)

Leetcode:找出数组中重复元素的题目总结 leetcode中找出数组元素的题目有如下几题: 645.Set Mismatch:https://leetcode.com/problems/set-mismatch/description/ 448.Find All Numbers Disappeared in an Array:https://leetcode.com/problems/find-all-numbers-disapp... 阅读详情

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~n范围内的数字,元素会出现1次或2次,找出出现2次的元素

思路:
因为不要有extra space而且是O(n) time。因此考虑在原数组上做,元素的范围在1~n,可以考虑把每个元素放到它对应的index的位置上,比如1就放在第1个,n放在最后一个。

遍历数组,调换位置,把元素放在应该在的位置。期间可能调换时会发现对应的index上已经是该元素,但这时不要把结果保存,因为可能重复,到全部调换完成之后,看对应位置上对不上的,就是结果。
比如[4,3,2,7,8,2,3,1]调换完后是[1, 2, 3, 4, 3, 2, 7, 8],找出nums[i] != (i+1)的保存到结果

注意:调换顺序时要先让nums[nums[i]-1] = nums[i]; 因为一旦nums[i]发生变化,后面nums[nums[i]-1]就不是正确的数字。

    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> result = new ArrayList<>();
        
        if(nums == null || nums.length <= 1) {
            return result;
        }
        
        int n = nums.length;
        
        for(int i = 0; i < n; i++) {
            
            while(nums[i] != (i+1)) {
                if(nums[i] == nums[nums[i]-1]) {
                    break;
                }
                int tmp = nums[nums[i]-1];
                nums[nums[i]-1] = nums[i];
                nums[i] = tmp;
            }
        }
        
        for(int i = 0; i < n; i ++) {
            if(nums[i] != (i+1)) {
                result.add(nums[i]);
            }
        }
        return result;
    }
【Array-medium】442. Find All Duplicates in an Array 找到数组中出现了两次的元素。 1. 题目原址 https://leetcode.com/problems/find-all-duplicates-in-an-array/ 2. 题目描述 3. 题目大意 找到数组中出现了两次的元素。 4. 解题思路 因为题意说数组的元素值的范围是: 1 ≤ a[i] ≤ n (n = size of array),。 所以,我们将数组值对应到新数组的元素下标,出现一次,值就+1, 最后判断... 阅读详情

相关推荐

详细解读文字识别工具———Tesseract-OCR

   在尝试破解12306验证码的时候,提前了解了一下应对各种验证码的解决方案,这里主要是学习到了面对文字图片验证码,例如亚马逊、豆瓣等平台的登录时会遇到的情况,效果还不错。 一、工具介绍 ​    Tesseract-OCR 是一款由HP实验室开发由Google维护的开源OCR(Optical Character Recognition...

别看我只是一只羊 2万+

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 spa

分享人工智能学习心得与实践经验,探讨应用场景,见证变革与进步 513

3台虚拟机部署Doris详细文档(含校验步骤)

摘要:本文详细介绍了Apache Doris在3台虚拟机上的集群部署方案。采用1主FE+1从FE+1观察者FE+3BE的混合部署模式,确保元数据高可用性。硬件配置建议测试环境4核16GB内存,生产环境8核64GB内存。部署步骤包括系统环境配置(关闭防火墙、内核参数优化)、FE/BE节点配置启动、集群初始化验证等。重点配置了IP白名单(172.30.0.115)和数据目录(/data/doris)。通过MySQL客户端和WebUI可监控集群状态,实现千万级数据的实时分析能力,后续可通过增加BE节点线性扩展。

weixin_41026747的博客 942

[leetcode] 442. Find All Duplicates in an Array

在数组中找出所有重复出现的数,要求时间复杂度O(n),空间复杂度O(1)

Halfopen的专栏 2173

MODBUS通讯不求人:手把手教你用三菱FX2N控制士林变频器

本文详细介绍了如何使用三菱FX2N PLC通过MODBUS协议控制士林变频器,涵盖硬件连接、参数配置、程序设计及通讯优化等关键步骤。特别针对没有内置MODBUS函数块的老款PLC,提供了CRC校验和稳定数据交换的实用解决方案,帮助工程师快速实现工业自动化设备的高效通讯。

v4w5x6的博客 241

从亚稳态到流片失败:SpyGlass CDC在5G芯片设计中的避坑指南

本文深入探讨了在复杂5G芯片设计中,如何利用SpyGlass CDC工具构建高效的时钟域交叉验证流程,以避免因亚稳态问题导致的流片失败。文章结合实战经验,详细解析了从约束编写、时钟复位完整性检查到层次化验证和特殊场景处理的全过程,并强调了精准的检查约束是确保验证有效性的基石,旨在帮助设计团队安全跨越异步时钟域的雷区。

weixin_29168153的博客 447

Find All Duplicates in an Array问题及解法

442. Find All Duplicates in an Array LeetCode

刘天的博客 354

【科研绘图】【数据拟合曲线】:附Origin详细画图流程

拟合曲线图

weixin_50236590的博客 4万+

LeetCode 442. Find All Duplicates in an Array

Problem Statement(Source) 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

浮生琴弦 2028

第01章 Hermes Agent 是什么

如果读者所在的开发团队已经引入了 Cursor、Claude Code 这类 AI IDE,可能会有一个困惑:写代码已经有 AI 帮忙了,还需要 Hermes Agent 做什么?这一章将给出答案。Hermes Agent 不是另一个写代码的 AI,而是一个常驻在团队里的"AI 协作成员",专门负责写代码之外的协作环节——讨论需求、审查代码、查日志、监控告警、生成报告。学完本章,读者将清楚 Hermes 的定位以及它在团队里的角色。

亢少军的博客 83

华为od 面试八股文_前端_03_含答案

华为od 面试八股文_前端_03_含答案

MISAYAONE的博客 2148

2017/11/20 Leetcode 日记

2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤n(n= size of array), some elements appeartwiceand others appearonce. Find all the elements th...

weixin_30832143的博客 108

webrtc进阶-- FlexFEC 实现原理与调试分析(m80 代码对齐版)

WebRTC 中 FlexFEC 技术解析与调试指南 摘要:本文深入分析 WebRTC 的 FlexFEC(灵活前向纠错)技术实现原理,重点探讨其在 m80 版本中的协商机制、接收侧恢复流程以及 SFU 场景下的特殊处理。文章指出 FlexFEC 通过动态冗余策略提升弱网抗丢包能力,详细解析了编解码器分配、SSRC 映射、包恢复等关键环节,并提供了针对 XR+SFU 场景的调试建议。调试时可重点关注 FEC 包接收与媒体包恢复日志,以验证链路有效性。

HOSTEN的博客 57

LeetCode //C - 442. Find All Duplicates in an Array

【代码】LeetCode //C - 442. Find All Duplicates in an Array。

Made in Code 1927

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode OJ 442. Find All Duplicates in an Array

LeetCode OJ 442. Find All Duplicates in an ArrayDescriptionGiven 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 app

jan_deng的博客 281

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

LeetCode刷题:442. Find All Duplicates in an Array

LeetCode刷题:442. Find All Duplicates in an Array 原题链接:https://leetcode.com/problems/find-all-duplicates-in-an-array/ Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 165

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

LeetCode: 442. Find All Duplicates in an Array

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 appe

qha0731的专栏 284

442. **Find All Duplicates in an Array

442. **Find All Duplicates in an Array https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appea...

珍妮的选择的博客 262

LeetCode 442——Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤n(n= size of array), some elements appeartwiceand others appearonce. Find all the elements that appeartwicein this array. Could you do it without extra ...

CrazyOnes的博客 335

[leetcode]442. Find All Duplicates in an Array(java)

问题: 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 wit

andy_gfz的博客 1446

leetcode 442. Find All Duplicates in an Array | 442. 数组中重复的数据(位运算)

题目 https://leetcode.com/problems/find-all-duplicates-in-an-array/ 题解 没想出来,看了评论之后写的,一语点醒。 思路就是,用num对应index的最高位表示是否出现过。 class Solution { public List<Integer> findDuplicates(int[] nums) { // when find a number i, flip the number at position

寒泉 4万+

【Leetcode】442. Find All Duplicates in an Array

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

zhizhi_zhizhi_zhizhi的博客 296

LeetCode 442 --- Find All Duplicates in an Array

题目链接: LeetCode 442 — Find All Duplicates in an Array问题分析: 需要o(n)的时间复杂度并且不能使用额外的存储空间。找出数组中的重复元素。AC代码:public class Problem442 { public static void main(String[] args) { // TODO Auto-generat

调包侠 830
上一篇: leetcode 209. Minimum Size Subarray Sum(最小size的子数组和)
下一篇: leetcode 565. Array Nesting(数组嵌套)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

蓝羽飞鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值
⌂ 换网址