[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]
思路:
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;
}
参考:
[leetcode-442-Find All Duplicates in an Array]的更多相关文章
- 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 ...
- 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 Array - 几种不同思路 - (C++)
题目 题目链接 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ...
- 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 ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
随机推荐
- 超简单jQuary链式操作代码实现手风琴效果
超简单jQuery代码实现手风琴效果 HTML代码 <div id="cont"> <div> <p>人生若只如初见</p> < ...
- [ios]quartz2d画板功功能实现核心代码
//触摸开始 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 1,获取对 ...
- 门面(Facade)模式
门面模式是对象的结构模式,外部与一个子系统的通信必须通过一个统一的门面对象进行.门面模式提供一个高层次的接口,使得子系统更易于使用. 医院的例子 现代的软件系统都是比较复杂的,设计师处理复杂系统的一个 ...
- 关于ONION/Wncry勒索病毒
1.通告 2017年5月12日起,在国内外网络中发现爆发基于Windows网络共享协议进行攻击传播的蠕虫恶意代码,这是不法分子通过改造之前泄露的NSA黑客武器库中“永恒之蓝”攻击程序发起的网络攻击事件 ...
- Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)
基于Spring-4.3.7.RELEASE Spring的配置不仅仅局限在XML文件,同样也可以使用Java代码来配置.在这里我使用XML配置文件的方式来粗略地讲讲WebApplicationCon ...
- 谈一谈JDK8的函数式编程 (一)
系列之前我想说的 最近有一段时间没写博客了,这几天回到学校,才闲下来,决定写一写最近学习到的只是,既是为了分享,也是为了巩固.之前看到过一篇调查,文章的数据是学习新知识,光是看只能获得大约5%,然 ...
- 关于Laravel中的artisan命令
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica; color: #454545 } p.p2 { margin: 0.0p ...
- 深度解析PHP数组函数array_chunk
array_chunk是PHP中的一个数组分割函数,是将一个数组分割为多个数组块 我们可以把它理解卖豆腐的商人把一整块大豆腐切割为一个一个的小块来进行售卖 这个函数需要三个参数: 被切割的数组(必需) ...
- [HDU1002] A + B Problem II
Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...
- RSA加密算法
class Program { static void Main(string[] args) { RSAPublicKey P = new RSAPublicKey(); P.Exponent = ...