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 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]
给定一个数组的整数,其中数组中的元素满足1 ≤ a[i] ≤ n ,n是数组的元素个数。一些元素出现了两次,而其它元素只出现了一次。
找出那些出现了两次的元素。要求没有占用额外的空间而且时间复杂性为 O(n) 。
例如:
输入:
[4,3,2,7,8,2,3,1]
输出:
[2,3]
思路
(不想看其它思路的可以直接跳到第三个)
其实本来一开始想到的实现方法是hash table的,但是如各位所知,hash table对空间还是有要求的,因此其实不符合题意,权当写着玩了。hash table的solution如下,runtime一般:
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
int m=nums.size(),n;
int s[m];
vector<int> ans;
memset(s,0,m*4);
for(int i=0;i<m;i++)
{
n=nums[i]-1;
s[n]++;
}
for(int i=0;i<m;i++)
if(s[i]>1) ans.push_back(i+1);
return ans;
}
};
第二个想法,先排序,再判断。runtime比较差,但空间占用少。
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
int m=nums.size(),n;
vector<int> ans;
stable_sort(nums.begin(),nums.end(),[](const int& x,const int& y){return x<y;});
for(int i=0;i<m;i++)
if(nums[i]==nums[i+1]) {ans.push_back(nums[i]);i++;}
return ans;
}
};
第三个想法,先归位,没在位置上的就是重复的数字。这个solution比之前的两个runtime表现都要好。
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
int m=nums.size(),n;
vector<int> ans;
for(int i=0;i<m;)
if(nums[nums[i]-1]!=nums[i])
swap(nums[i],nums[nums[i]-1]);
else i++;
for(int i=0;i<m;i++)
if(nums[i]!=i+1) ans.push_back(nums[i]);
return ans;
}
};
LeetCode - 442. Find All Duplicates in an Array - 几种不同思路 - (C++)的更多相关文章
- 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 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 ...
随机推荐
- 由Oracle 11g SYSAUX 和 SYSTEM 表空间回收引发的联想
0x00--目的 整理一下以前一个SYSTEM表空间和SYSAUX表空间使用率达到99%上限的处理思路和相关知识点,好记性不如烂笔头 0x01--表空间使用率现状 通过查询可得知目前表空间使用情况如下 ...
- android软件开发之获取本地音乐属性
歌曲的名称 :MediaStore.Audio.Media.TITLString tilte = cursor.getString(cursor.getColumnIndexOrThrow(Media ...
- 闲谈Hybrid
前言 当经常需要更换样式,产品迭代,那么我们应该考虑hybrid混合开发,上层使用Html&Css&JS做业务开发,底层透明化.上层多多样化,这种场景非常有利于前端介入,非常适合业务快 ...
- Zabbix——使用邮件报警
前提条件: 1. Zabbix版本4.0 zabbix-server 命令配置: yum install mailx -y #下载邮件功能 vi /etc/mail.rc set bsdcompat ...
- 爬虫——Scrapy框架案例一:手机APP抓包
以爬取斗鱼直播上的信息为例: URL地址:http://capi.douyucdn.cn/api/v1/getVerticalRoom?limit=20&offset=0 爬取字段:房间ID. ...
- 记一次MySQL中Waiting for table metadata lock问题的处理
起因:由于需要,要把一张表的一个字段从不是 null 改成 可null,我用的Navicat Premium ,但是在保存的时候,工具无响应了,几个同事操作都是这样的,很奇怪,怀疑是不是由于表被锁了还 ...
- JQuery制作网页——第五章 初识 jQuery
1.jQuery简介: ● jQuery由美国人John Resig于2006年创建 ● jQuery是目前最流行的JavaScript程序库,它是对JavaScript对象和函数的封装 ● 它的设计 ...
- TinyMCE插件:RESPONSIVE filemanager 9 文件名统一格式化
上传图片方法(filemanager/UploadHandler.php) 在上传图片的函数中查看,发现$file->name是一个完整的[文件名.后缀名],所以使用explode(),文件名和 ...
- Vagrant 总结
引言 本文将讨论Vagrant基础应用,高级应用,基于Vagrant 的虚拟机优化,Vagrant的优势及区别等四部分 参考资料: [阿里云教程中心]Docker学习总结之Docker与Vag ...
- 【AD】自己画板的备忘
快捷键: [Ctrl + M ]计算出两点之间的距离,画电路板时会用到 [Ctrl + Q ]在设定X.Y..等等的地方,快捷键可以公英制快速切换 [shift + 空格键 ]在布线的同时,此快捷键可 ...