442 Find All Duplicates in an Array 数组中重复的数据
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
示例:
输入:
[4,3,2,7,8,2,3,1]
输出:
[2,3]
详见:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/
C++:
方法一:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i=0;i<nums.size();++i)
{
int idx=abs(nums[i])-1;
if(nums[idx]<0)
{
res.push_back(idx+1);
}
nums[idx]=-nums[idx];
}
return res;
}
};
方法二:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums)
{
vector<int> res;
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != nums[nums[i] - 1])
{
swap(nums[i], nums[nums[i] - 1]);
--i;
}
}
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != i + 1)
{
res.push_back(nums[i]);
}
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/6209746.html
442 Find All Duplicates in an Array 数组中重复的数据的更多相关文章
- LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17
442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n ...
- Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)
题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...
- Java实现 LeetCode 442 数组中重复的数据
442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- [Swift]LeetCode442. 数组中重复的数据 | 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 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 ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 442. Find All Duplicates in an Array - LeetCode
Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...
- php去除数组中重复数据
<?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...
随机推荐
- oracle 重要函数
1)instr()函数的格式 (俗称:字符查找函数) 格式一:instr( string1, string2 ) / instr(源字符串, 目标字符串) 格式二:instr( string1, st ...
- 嵌入式开发之davinci--- 8168 电源调试总结
http://www.61ic.com/Article/DaVinci/TMS320DM81x/201403/51863.html
- 【iOS系列】-使用CAGradientLayer设置渐变色
有时候iOS开发中需要使用到渐变色,来给图片或者view盖上一层,使其显示效果更好,我们这里使用的CAGradientLayer来设置渐变色 要实现的效果如下: Demo地址---下载 // 创建渐变 ...
- Apache Karaf配置远程调试
软件环境 apache-karaf-4.0.0 配置方法: 在 bin/karaf.bat 文件里,顶部增加 set KARAF_DEBUG=true 然后.重新启动karaf 启动之后.就可以看到例 ...
- flask的CBV,flash,Flask-Session,及WTForms-MoudelForm
1,CBV: from flask import vews class LoginView(views.MethodView): def get(self): return "雪雪其实也很好 ...
- Deep Learning 29: caffe入门学习
1.跑教程:深度学习(六)caffe入门学习,上面有比较好的注释 .prototxt文件:网络结构文件 solver.prototxt:网络求解文件 net: "examples/mnist ...
- RabbitMQ(三)RabbitMQ消息过期时间(TTL)
在RabbitMQ(二)AMQP协议mandatory和immediate标志位区别中我们提到,在RabbitMQ3.0以后的版本里,去掉了immediate参数支持,要实现类似的确认功能要使用TTL ...
- I.MX6 天嵌 E9 U-boot menu hacking
/************************************************************************************ * I.MX6 天嵌 E9 ...
- 并不对劲的bzoj5340:loj2552:uoj399:p4564: [Ctsc2018]假面
题目大意 有\(n\)(\(n\leq200\))个非负整数\(m_1,m_2,...,m_n\)(\(\forall i\in[1,n],m_i\leq100\)),有\(q\)(\(q\leq2* ...
- SPOJ:Triple Sums(母函数+FFT)
You're given a sequence s of N distinct integers.Consider all the possible sums of three integers fr ...