[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 ...
随机推荐
- 转:Linux中文显示乱码?如何设置centos显示中文
怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导 ...
- Html标签,file方式,上传文件
恩,如果不记下来,记忆就会模糊掉. 希望自己下次看见这篇博客的时候,会解决掉疑问 ----------------------------------------------------------- ...
- PHP如何与搜索引擎Elasticsearch交互?
一:参考官方文档 1. Elasticsearch 5.4.0英文手册:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/sear ...
- Word Ladder 2015年6月3日
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- ListView的使用(一)
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. 列表的显示需要三个元素: 1.ListVeiw 用来展示列表的View. 2.适 ...
- 小程序的1024KB
1024kb 只是一个目前编译后代码可上传最大限制,至于后期会不会更改,不得而知.个人只是想借 1024kb 来和大家一起交流一下,如何在限制下,挥舞大刀- 微信官方回答了,为什么有 1024kb 的 ...
- Linux下Samba服务器的安装和配置
第一步:sudo apt-get install samba smbclient 安装samba服务器. 第二步:打开/etc/samba/smb.conf文件,在末尾添加下面的字段: [用户名] c ...
- Java反射机制剖析(二)-功能以及举例
从<java反射机制剖析(一)>的API我们看到了许多接口和类,我们能够通过这些接口做些什么呢? 从上篇API中我们能看到它能够完成下面的这些功能: 1) 获得类 A. 运 ...
- 浅析Web缓存
在前端开发中,性能一直都是被大家所重视的一点,然而判断一个网站的性能最直观的就是看网页打开的速度.其中提高网页反应速度的一个方式就是使用缓存.一个优秀的缓存策略可以缩短网页请求资源的距离,减少延迟,并 ...
- 2016计蒜之道复赛B题:联想专卖店促销
题解 思路: 二分答案,设我们要check的值为x. 注意到每一个礼包都有,一个U盘,一个鼠标. 剩余的,分别为一个机械键盘,一个U盘,一个鼠标. 当礼包数目为x时,我们至多可以提供a-x个普通,b- ...