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]

题目标签:Array
  题目给了我们一个nums array,其中有一些数字出现两次,剩下的都只出现一次,让我们把重复两次的都找出来。
  因为这道题目没有规定我们不能改动 array,所以可以把array 里的 num 和 在 nums[ num - 1 ]  做一对一的映射,每次遇到一个num, 把对应位置上的 num 改成 -num 标记。如果遇到一个重复项,那么肯定已经有同样的数字来标记过了。
 

Java Solution:

Runtime beats 85.92%

完成日期:09/19/2017

关键词:Array

关键点:把num 和 nums[num - 1] 做1对1的映射

 class Solution
{
public List<Integer> findDuplicates(int[] nums)
{
List<Integer> duplicates = new ArrayList<>(); for(int num: nums)
{
int absNum = Math.abs(num); if(nums[absNum - 1] < 0) // if the number at position num - 1 is already negative
duplicates.add(absNum); // num is duplicate
else
nums[absNum - 1] *= -1;
} return duplicates;
}
}

参考资料:

https://discuss.leetcode.com/topic/64735/java-simple-solution

LeetCode 题目列表 - LeetCode Questions List

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)的更多相关文章

  1. leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项

    https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...

  2. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  3. LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)

    题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  6. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  7. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  8. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  9. 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

    乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言     我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...

随机推荐

  1. Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】

    对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...

  2. temp-存储过程 以前的

    ---------------------------------------------------------------------------------------------------- ...

  3. 传统编程和IoC的对比

    ref:http://www.importnew.com/13619.html 传统编程:决定使用哪个具体的实现类的控制权在调用类本身,在编译阶段就确定了. IoC模式:调用类只依赖接口,而不依赖具体 ...

  4. 检测Linux硬盘IO数据

    首先安装sysstat(有些系统会预装): apt install sysstat 然后使用命令: iostat -x -k 2 x选项用于显示和io有关的扩展数据 k某些使用block为单位的字段改 ...

  5. 可能是讲解ARM中断和中断嵌套最通俗易懂的文章

    几天前一个学生问我ARM中断嵌套的问题,我才发现原来在我心中理所当然的事对学生来说理解实属不易.  ARM有七种模式,我们这里只讨论SVC.IRQ和FIQ模式.  我们可以假设ARM核心有两根中断引脚 ...

  6. 指定路径下建立Access数据库并插入数据

    今天刚刚开通博客,想要把我这几天完成小任务的过程,记录下来.我从事软件开发的时间不到1年,写的不足之处,还请前辈们多多指教. 上周四也就是2016-04-14号上午,部门领导交给我一个小任务,概括来讲 ...

  7. python 脚本开发实战-当当亚马逊图书采集器转淘宝数据包

    开发环境python2.7.9 os:win-xp exe打包工具pyinstaller 界面tkinter ============================================= ...

  8. RAID及热备盘详解

    RAID,为Redundant Arrays of Independent Disks的简称,中文为廉价冗余磁盘阵列. 一.出现的原因(RAID的优点): 它的用途主要是面向服务器,但现在的个人电脑由 ...

  9. try catch finally 中包含return的几种情况,及返回结果

    当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况:在try和catch中有return,finally中没有return,且finally中没有对try或catch中要 retu ...

  10. HTTP错误代码大全

    HTTP出错大全 101 - Switching Protocols Top Success Codes 200 - OK201 - Created202 - Accepted203 - Non-Au ...