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:

  1. Input:
  2. [4,3,2,7,8,2,3,1]
  3.  
  4. Output:
  5. [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的映射

  1. class Solution
  2. {
  3. public List<Integer> findDuplicates(int[] nums)
  4. {
  5. List<Integer> duplicates = new ArrayList<>();
  6.  
  7. for(int num: nums)
  8. {
  9. int absNum = Math.abs(num);
  10.  
  11. if(nums[absNum - 1] < 0) // if the number at position num - 1 is already negative
  12. duplicates.add(absNum); // num is duplicate
  13. else
  14. nums[absNum - 1] *= -1;
  15. }
  16.  
  17. return duplicates;
  18. }
  19. }

参考资料:

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. Spring security csrf实现前端纯html+ajax

    spring security集成csrf进行post等请求时,为了防止csrf攻击,需要获取token才能访问 因此需要添加 <input type="hidden" na ...

  2. Excel 若某列包含某个字符则特定列显示特定字符

    今天在写Excel , 有很多重复的数据, 在想 如果 可以像Java  一样 筛选就好了 这样的效果 if ("adj".equals(sheet1.A1)){ sheet1.B ...

  3. GitHub新手使用教学(从安装到使用)

    一.安装Git for Windows(又名msysgit)  下载地址: https://git-for-windows.github.io/  在官方下载完后,安装到Windows Explore ...

  4. angular 自定义指令详解 Directive

    在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...

  5. NameError: name 'messagebox' is not defined 错误处理

    写了段代码,想在按下button的时候跳个提示框出来,调试的时候提示了messagebox未定义 from tkinter import * def test_show(): messagebox.s ...

  6. C#设计模式之四抽象工厂模式(AbstractFactory)【创建型】

    一.引言     写了3篇有关设计模式的文章了,大家有了些反馈,说能从中学到一些东西,我感到很欣慰,那就继续努力.今天我要写第四个模式了,该模式叫抽象工厂.上一篇文章我们讲了[工厂方法]模式,它是为了 ...

  7. Xcode 上传代码到GitHub

    几乎所有iOS程序员都上过GitHub寻找开源类库,的确,GitHub上有大量优秀的开源类库供大家学习.但是如何在Xcode中上传代码至GitHub呢? (开始之前先安装git,具体方法这里讲的很清楚 ...

  8. jdk版本查看,以及java -version 和JAVA_HOME不一致问题

    一.jdk版本查看及位数查看 在cmd进入命令行窗口,输入java -version 可以查看安装的jdk版本,如图: 当有64-bit时代表是64位jdk,如果没有则默认是32位的. 二.java ...

  9. Python CSV 超简明用法

    平常经常会用CSV存储数据,不可避免的会跟CSV文件的读写操作扯上关系. Python有CSV这个Package来解决这个问题,官网也有比较详细的教程 https://docs.python.org/ ...

  10. Springboot 学习笔记 ①

    前言 之前一直在寻找Springboot的学习资料,终于得偿所愿...那么,先给自己定一个小目标 - 能够使用Springboot这套架构来搭建自己的服务. 准备阶段 1. 开发环境 开发环境其实还是 ...