442. Find All Duplicates in an Array

Add to List

Description Submission Solutions

  • Total Accepted: 16589
  • Total Submissions: 32282
  • Difficulty: Medium
  • Contributors: shen5630

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]

Subscribe to see which companies asked this question.

【题目分析】

给定一个整数数组,数组中的元素都满足1 =< a[i] <= n。找出数组中重复出现的元素。要求不使用额外的存储空间,算法的时间复杂度是O(n)。

【思路分析】

对数组中的每个元素,把它当作数组的索引,把该索引对应位置的元素取反,如果发现该位置的元素已经取反,那么这个数就是重复出现的数。

【java代码】

 1 public class Solution {
 2     public List<Integer> findDuplicates(int[] nums) {
 3         List<Integer> res = new ArrayList<Integer>();
 4
 5         for(int i = 0; i < nums.length; i++) {
 6             int index = Math.abs(nums[i]) - 1;
 7             if(nums[index] < 0) {
 8                 res.add(index+1);
 9             }
10             nums[index] = -nums[index];
11         }
12
13         return res;
14     }
15 }

转载于:https://www.cnblogs.com/liujinhong/p/6416666.html

LeetCode 442. Find All Duplicates in an Array相关推荐

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

    题目 https://leetcode.com/problems/find-all-duplicates-in-an-array/ 题解 没想出来,看了评论之后写的,一语点醒. 思路就是,用num对应 ...

  2. LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array

    这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...

  3. 【leetcode】442. Find All Duplicates in an Array(Python C++)

    442. Find All Duplicates in an Array 题目链接 442.1 题目描述: Given an array of integers, 1 ≤ a[i] ≤ n (n = ...

  4. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  5. 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 ...

  6. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  7. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  8. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  9. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

最新文章

  1. Win7下JDK环境变量设置批处理(转)
  2. 服务器 声音文件 nginx,docker nginx搭建视频音频服务器
  3. 剑指offer 算法 (举例让抽象具体化)
  4. echart折线图小知识
  5. c语言考试算法,c语言考试常用算法docx.docx
  6. 我的世界服务器自定义欢迎界面,我的世界启动界面如何修改 diy修改启动界面攻略...
  7. JEECG 3.6.3版本发布 企业级JAVA快速开发平台
  8. 数字图像处理与机器视觉光盘资料_机器视觉——数字图像处理知识点总结
  9. .net java 开源_为什么说微软开源 .net 也拼不过 java?
  10. tensorflow 遇到的细节问题
  11. 腾讯云服务器的环境配置与使用
  12. 2018年版北大中文核心期刊目录(2017最新第八版)
  13. 『拓扑排序』「NOI2010」航空管制
  14. python docx首行缩进两字符的设定方法
  15. RxHttp 全网Http缓存最优解,完整PDF
  16. 大工21春《土木工程实验(二)》实验报告
  17. 健身网站php,基于WEB的健身网站的设计与实现
  18. Java虚拟机-JVM
  19. 如何在云服务器安装操作系统
  20. TDH中的Workflow

热门文章

  1. maven3.5.0在win10中的安装及环境变量配置
  2. 即时通讯软件设计(一)
  3. POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘...
  4. 设置虚拟机和本机时间同步方法
  5. python学习之--安装IDE(eclipse+pydev)
  6. oracle中substr() instr() 用法
  7. Ubuntu+Apache+PHP+Mysql环境搭建
  8. JavaScript面向对象编程(1)-- 基础
  9. 如果连接远程的mysql数据库连不上,可用下面的方法试下
  10. Java基础-关于session的详细解释