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 ...
随机推荐
- Reading Notes : 180215 计算机系统
读书<计算机组成原理>,<鸟哥的Linux私房菜 基础篇>百度百科,内容摘自<计算机组成原理>,<鸟哥的Linux私房菜 基础篇> 计算机系统 在前面几 ...
- UE4 Navmesh 室内导航设置
我用的UE版本是4.14.1 系统:win10 64 前不久给样板房里面做了一个扫地机器人,导航设置让我头大了很久,度娘也没有用,最后在谷哥上有所感悟,现在给出本人的设置过程和解决方案. 一开始拖 ...
- Linux -- 用户组篇
Linux -- 用户与用户组 1.Linux 系统中有三种角色:所有者(用户),用户组与其他人,一张图可以说明用户与用户组的关系. 如图,某公司相当于一个用户组,该用户组下有A,B两个用户,用户拥有 ...
- JavaScript常用DOM操作方法和函数
查找节点ocument.querySelector(selectors) //接受一个CSS选择器作为参数,返回第一个匹配该选择器的元素节点.document.querySelectorAll(sel ...
- MySQL必知必会 读书笔记四:数据过滤
过滤数据 WHERE 只检索所需数据需要指定搜索条件( search criteria) ,搜索条件也称为过滤条件( filtercondition) . 在SELECT语句中,数据根据WHERE子句 ...
- PHP变量定义及工作原理
1.变量定义: 通常学到的是,变量代表存储空间以及其中数据的一个“标识符”. 变量名 指向 变量值 更深入的说是 变量指向内存的一块区域 2.变量工作原理,通过画图分析法——内存空间 <?php ...
- Java客户端访问HBase集群解决方案(优化)
测试环境:Idea+Windows10 准备工作: <1>.打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户 ...
- ArrayList的源码分析(基于jdk1.8)
1.初始化 transient Object[] elementData; //实际存储元素的数组 private static final Object[] DEFAULTCAPACITY_EMPT ...
- php+IIS 配置环境(windows环境)
继php7+apache2.4 配置环境(window环境)后,由于B2C项目准备上线:特此小编在阿里云上搭建PHP7环境,为此特写上搭建过程希望正处于搭建php7+IIS(windows环境)中的朋 ...
- Redis的n种妙用,不仅仅是缓存
redis是键值对的数据库,常用的五种数据类型为字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset) Redis用作缓存,主要两个用途: ...