[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 ...
随机推荐
- NSA永恒之蓝病毒,如何通过360工具修复?
简介: NSA武器库的公开被称为是网络世界"核弹危机",其中有十款影响Windows个人用户的黑客工具,包括永恒之蓝.永恒王者.永恒浪漫.永恒协作.翡翠纤维.古怪地鼠.爱斯基摩卷. ...
- Day1-三元运算及
三元运算:result = Value1 if Condition else Vlaue2 >>> a,b,c = 1,3,5>>> d = a if a > ...
- 关于QT按键信号槽的总结(原创)
QT界面按钮一般是必填的: 每个按钮都要 Go to slot 下面有几个都是常用的,先说一下 clicked:pressed:releaed的区别 字面意思看:click是点击一下,pressed是 ...
- MySQL全文检索初探
本文目的 最近有个项目需要对数据进行搜索功能.采用的LAMP技术开发,所以自然想到了MySQL的全文检索功能.现在将自己搜集的一些资料小结,作为备忘. MySQL引擎 据目前查到的资料,只有MyISA ...
- 4.docker学习之镜像
镜像 我们知道,我们想在Windows操作系统上跑Linux,需要安装一个虚拟机程序,然后下载一个Linux镜像,在该虚拟机程序中创建一个虚拟机,并使用该镜像安装对应的Linux操作系统,安装好之后, ...
- WinForm笔记
Hi All, 分享一个学WinForm时的笔记: 1. 关键字 partial:是部分类,允许将一个类放在多个文件当中. 2. MessageBox()类相当于Console.WriteLine() ...
- springcloud(七):配置中心svn示例和refresh
上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...
- 深入理解C#中的String
关于C#中的类型 在C#中类型分为值类型和引用类型,引用类型和值类型都继承自System.Object类,几乎所有的引用类型都直接从System.Object继承,而值类型具体一点则继承System. ...
- root用户不能修改iptable文件
问题: 需要放通IP 端口 执行: vi /etc/sysconfig/iptables, 添加完成后,wq保存,提示文件只读无法保存!!! 解决步骤: 1.查看文件权限 ls -ld /etc/ ...
- Graphical Analysis of German Parliament Voting Pattern
We use network visualizations to look into the voting patterns in the current German parliament. I d ...