题目

题目链接

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++)的更多相关文章

  1. 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 ...

  2. leetcode 442. Find All Duplicates in an Array 查找数组中的所有重复项

    https://leetcode.com/problems/find-all-duplicates-in-an-array/description/ 参考:http://www.cnblogs.com ...

  3. 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 ...

  4. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  5. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  6. 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

    乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言     我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...

  7. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  8. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  9. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

随机推荐

  1. private、protected、public和internal的区别

    private是完全私有的,只有在类自己里面可以调用,在类的外部和子类都不能调用,子类也不能继承父类的private的属性和方法. protected虽然可以被外界看到,但外界却不能调用,只有自己及自 ...

  2. 【Linux资源管理】使用sar进行性能分析

    sar可用于监控Linux系统性能,帮助我们分析性能瓶颈.sar工具的使用方式为”sar [选项] intervar [count]”,其中interval为统计信息采样时间,count为采样次数. ...

  3. oracle查询相关注意点

    单表查询: .or 和 and 混合使用 需求:查询业主名称包含'刘'或门牌号包含'5'的,并且地址编号为3的记录 and 的权限优先于 or 所以需要在or的两边添加() 2. 范围查询 除了传统的 ...

  4. 【读书笔记】The Swift Programming Language (Swift 4.0.3)

    素材:Language Guide 初次接触 Swift,建议先看下 A Swift Tour,否则思维转换会很费力,容易卡死或钻牛角尖. 同样是每一章只总结3个自己认为最重要的点.这样挺好!强迫你去 ...

  5. 利用MFC Picture Control控件 加载bmp,png

    1.在资源视图,选择PictureControl,并且在属性中把Type设置为Bitmap. 2.加载PNG CStatic* pWnd = (CStatic*)GetDlgItem(IDC_PIC) ...

  6. MySQL---存储过程 及 条件语句、循环语句

    存储过程 存储过程是一个SQL语句集合,当主动去调用存储过程时,其中内部的SQL语句会按照逻辑执行. 1.创建存储过程 -- 创建存储过程 delimiter // create procedure ...

  7. 关于css属性calc对于ie的态度

    做的一个项目,布局的时候用到了max-height:calc(100% - 15px);在谷歌.火狐浏览器,进行下拉的时候,它的父元素会出现垂直滚动条,但是在IE就不可以. 然后在网上找了找,说在它的 ...

  8. 【Java】abstract,final,static,private,protected,public的区别

    [abstract]抽象的 1. abstract可以修饰类和成员方法,被abstract修饰的类称为抽象类,被abstract修饰成员方法叫抽象方法.抽象类不一定有抽象方法,但拥有抽象方法的类一定是 ...

  9. Home Assistant系列美化篇——替换天气 UI

    替换天气组件 weather 的默认 UI,生成美观大方的气象卡片. Home Assistant 原生的天气平台不少,国内用户常用的有雅虎天气和 Darksky.其他论坛和社区也有分享自制的和风.彩 ...

  10. KEIL MDK-ARM Version 5.26正式版开发工具下载

    Keil MDK最新版本已经出来啦,ARM开发工具MDK-ARM Version 5.26地址:http://www.myir-tech.com/soft.asp?id=1141,需要的可以去下载哦 ...