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, 最后判断... 阅读详情

相关推荐

智能车竞赛走马观碑组:如何高效识别目标板并优化绕行策略(附实战代码)

本文深入探讨了智能车竞赛走马观碑组中,如何构建从图像识别到路径决策的全栈优化方案。针对目标板识别与绕行策略的核心难点,文章详细解析了基于MobileNetV2的高效识别流水线、结合预瞄点与纯追踪算法的平滑路径规划,以及横向与纵向协同控制策略,并提供了实战代码与调试指南,旨在全面提升小车的比赛表现。

o5p6q的博客 883

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

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

图解转置卷积原理

声明:本文大部分搬运自【机器学习】详解 转置卷积 (Transpose Convolution) 感谢博主花与人间事同,本人只是在博主原创文章上做补充以及添加一些自己的理解。 1 转置卷积的背景 通常,对图像进行多次卷积运算后,特征图的尺寸会不断缩小。而对于某些特定任务 (如图像分割和图像生成等),需将图像恢复到原尺寸再操作。这个将图像由小分辨率映射到大分辨率的尺寸恢复操作,称为上采样 (Upsample),如下图所示: 上采样方法有很多,详见【图像处理】详解 最近邻插值、线性插值、双线性插值、双三次插值

laonafahaodange的博客 5684

leetcode442 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 without ex

店小不二的博客 590

ANSYS Twin Builder实战:5步搞定数字孪生模型部署(附ROM配置技巧)

本文详细解析了ANSYS Twin Builder在数字孪生模型部署中的实战应用,重点介绍了ROM(降阶模型)配置技巧。通过5个关键步骤,从环境搭建到工业部署,帮助工程师高效实现高精度三维仿真到轻量化数字孪生的转化,提升预测性维护和系统仿真效率。

weixin_27330481的博客 655

空间数据库(1):Arcgis10.2连接PostgreSQL9.1,创建空间数据库,导入空间数据

1、arcgis的安装对于gis人员来说想必并不陌生,此处就不赘述了。那么就先从PostgreSQL说吧,我这里用的是跟Arcgis desktop 10.2所支持的9.1或者9.2版本,大家也可以用相对应的更高的版本来配置,注意的是版本一定要相互匹配,不仅是Arcgis与PostgreSQL的匹配,还有PostgreSQL与PostGIS匹配。 按道理desktop是32位的,应该用PostgreSQL的32位,但是我实在找不到32位的PostGIS9.1,所以就用64位的吧,后面配置部分可能需

XIAOHUA的博客 1317

【LeetCode】442. Find All Duplicates in an Array 数组中重复的数据

【LeetCode】442. Find All Duplicates in an Array 解题报告 标签: LeetCode 题目地址:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 题目描述: Given an array of integers, 1 ≤ a[i] ≤ n

负雪明烛 2158

从时序迷宫到稳定通信:FSMC配置中的时间参数深度解析与实战避坑指南

本文深入解析STM32 FSMC配置中的时间参数,特别是AddressSetupTime和DataSetupTime的实战配置技巧。通过STM32F407连接SRAM的实际案例,提供从理论计算到CubeMX配置的全流程指南,帮助开发者避免常见时序陷阱,确保稳定通信。

nginx7reverse的博客 81

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的博客 106

[特殊字符] RK3588 平台上基于 Panfrost 的 Mesa 驱动编译全过程

摘要: 本文详细记录了在RK3588平台(Ubuntu 20.04 aarch64)上编译基于Panfrost的Mesa驱动的全过程。内容包括:1)系统依赖安装;2)libdrm编译;3)Mesa配置参数优化;4)LLVM 15与SPIRV工具链的安装方法;5)SPIRV-Tools版本问题的解决方案;6)最终编译命令及常见错误处理。关键点在于正确设置工具链路径(PKG_CONFIG_PATH)和版本匹配(LLVM 15对应SPIRV-LLVM-Translator v15.0.0),推荐将组件安装在/op

梁俊星的博客 938

R语言Rstudio突然无法启动且报错:ERROR system error 42 [str: 一, len: 3, from: UTF-8, to: ISO8859-1]...如何解决

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 2846

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

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

Made in Code 1924

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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的博客 280

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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的博客 331

[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的博客 1445

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

调包侠 828
上一篇: 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、付费专栏及课程。

余额充值