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]

思路:

The concept here is to negate each number's index as the input is 1 <= a[i] <= n (n = size of array).

Once a value is negated, if it requires to be negated again then it is a duplicate.

vector<int> findDuplicates(vector<int>& nums)
{
vector<int>ret;
for (int i = ; i < nums.size();i++)
{
int index = abs(nums[i]) - ;
if (nums[index] > )
{
nums[index] = - nums[index];
}
else
{
ret.push_back(abs(nums[i]));
}
}
return ret;
}

参考:

https://discuss.leetcode.com/topic/64805/java-easy-to-understand-solution-without-extra-space-and-in-o-n-time

[leetcode-442-Find All Duplicates in an Array]的更多相关文章

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

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

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

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

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

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

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

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

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

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

  8. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

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

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

  10. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

随机推荐

  1. JDBC事务详解

    在JDBC的数据库操作中,事务是由一个或者多个操作组成的一个不可分割的工作单元.JDBC的事务处理包含三个方面:事务的自动提交模式(Auto-commit mode).事务隔离级别(Transacti ...

  2. jquery之属性操作

    jQuery之属性操作 相信属性这个词对大家都不陌生.今天我就给大家简单地介绍一下JQuery一些属性的操作 属性一共分三大类 一.基本属性 1.attr 2.removeAttr 3.prop 4. ...

  3. HDU1829(种类并查集)

    ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexua ...

  4. CoreOS, Kubernetes, etcd

    CoreOS CoreOS Container Linux is the leading container operating system, designed to be managed and ...

  5. 解决Cornerstone不能上传.a文件的问题 Cornerstone不上传*.xcuserstate,*.xcbkptlist文件

    在使用CornerStone的时候经常会出现.a文件无法上传的问题,导致从svn checkout到本地的时候编译报错 这里可以通过配置CornerStone来达到上传.a文件的效果 操作步骤: 打开 ...

  6. 面试(1)-java-se-字符串

    http://blog.csdn.net/zhangerqing/article/details/8093919 hashCode和identityHashCode的区别 I. hashCode()方 ...

  7. 【.net 深呼吸】通过标准输入/输出流来完成进程间通信

    实现进程之间煲电话粥的方式,有好几种,比如,你可以用这些方案: 1.使用socket来传递.这个好像很无聊,本地进程之间也用socket?不过,通过本机回环网络确实可以进程之间通信. 2.WCF,与上 ...

  8. linux新学篇

    [学会使用快捷键] Ctrl + C:这个是用来终止当前命令的快捷键,当然你也可以输入一大串字符,不想让它运行直接Ctrl + C,光标就会跳入下一行. Tab: 这个键是最有用的键了,也是笔者敲击概 ...

  9. 02-2--数据库MySQL:DDL(Data Definition Language:数据库定义语言)操作数据库中的表(二)

    DDL对数据库的操作:http://blog.csdn.net/baidu_37107022/article/details/72334560 DDL对数据库中表的操作 1)方法概览 2)演示 //创 ...

  10. Java中IO流的总结

    有关Java中IO流总结图 流分类 按方向分 输入流 输出流 按单位分 字节流 字符流 按功能分 节点流 处理流(过滤流) 其他 所有的流继承与这四类流:InputSteam.OutputStream ...