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 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]
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 (在数组中找到所有的重复项)的更多相关文章
- leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项
https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...
- Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...
- 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 ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [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 ...
- 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 ...
- 442. Find All Duplicates in an Array - LeetCode
Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言 我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...
随机推荐
- Java:final、static关键字 详解+两者结合使用
一 final关键字 1) 关于final的重要知识点 final关键字可以用于成员变量.本地变量.方法以及类. final成员变量必须在声明的时候初始化或者在构造器中初始化,否则就会报编译错误. ...
- 更自然的解决字典数组插入nil而导致crash
最近在优化项目虽说小优化一直在持续,大版本的优化也进行了两个版本了但是bug列表依旧血淋淋的摆在那里.有的看一眼也能找到问题所在但是有的就是想破头也不知道问题在哪里,毕竟整个项目经过了N个人的手代码风 ...
- Python数据分析numpy库
1.简介 Numpy库是进行数据分析的基础库,panda库就是基于Numpy库的,在计算多维数组与大型数组方面使用最广,还提供多个函数操作起来效率也高 2.Numpy库的安装 linux(Ubuntu ...
- 来自projecteuler.net网站的练习题1
0.题目如下: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prim ...
- [python学习笔记] pyinstaller打包pyqt5程序无法运行
问题 pyinstaller打包的pyqt5程序在部分电脑上会失败.用户截图提示下边错误日志 无法定位程序输入点 ucrtbase.terminate 于动态链接库 api-ms-win-crt-ru ...
- 这是一名Java学者关于学习方向的建议
无可厚非,编程是一门艺术.但是辉煌的背后必须是一段辛苦的奋斗过程,而过程的引导方向就是最重要的一环.Java语言可谓是引领了编程的潮流,你会是怎样去学的呢? 这是一名Java学者的学习方向的建议 注: ...
- 都是Javascript的作用域惹得祸
案件重现 今天有位然之OA 系统的定制开发用户咨询了个问题,他想在新加的功能模块的操作面板中,实现用户点击删除按钮时提示友好提醒,如下: 问题很简单,虽然他自己最终达到目的效果了,但不知道起初问题出在 ...
- 多线程简单实例(1)真的需要synchronized么?
说道多线程的安全问题,很多人想到就就是加锁.用到synchronized关键字. 那就要先说说synchronized问什么能够保证线程安全了. 首先要了解线程的工作方式:线程工作分为工作内存和主内存 ...
- AngularJS–Animations(动画)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 在AngularJS 1.3 中,给一些指令(eg: ngRepeat,ngSw ...
- Quartz学习——Spring和Quartz集成详解(三)
Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...