题目

题目链接

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. Knowledge Point 20180303 我们为什么要配置环境变量

    1.1为什么要配置环境变量 在学习JAVA的过程中,涉及到多个环境变量(environment variable)的概念,如PATH.正确地配置这些环境变量,是能够顺利学习.开发的前提.而经常出现的问 ...

  2. Oracle在线重定义(online redefinition)--将普通表改为分区表

    使用Oracle的在线重定义技术,可以将Oracle的普通表改为分区表.操作如下: STEP1:测试表是否可以在线重定义,这里以unixdev数据库的LIJIAMAN.BSTEST为例 EXEC DB ...

  3. oracle在线迁移同步数据,数据库报错

    报需要升级的错误,具体处理步骤如下: 一.错误信息 SQL> alter database open ;alter database open resetlogs*ERROR at line 1 ...

  4. HTML中IMG标签总结

    一.Img标签有两个重要的属性: 1.src  属性:图片的地址 2.alt  属性:图片不显示是现实的文字 二.Img标签是行级元素: img.input属于行内替换元素.height/width/ ...

  5. OC中自定义init方法

    ---恢复内容开始--- 我们知道,在函数中实例化一个对象,大多数会同时进行初始化,如 Person *p =[ [Person alloc]init]; 此时已经进行了初始化,使用init方法,那么 ...

  6. GPUImage源码解读之GPUImageFramebuffer

    简介 OpenGL ES的FrameBuffer是渲染发生的地方,普通的2D图形的渲染默认发生在屏幕上:而三维的图形渲染则除了包括像素点的颜色,还有Depth Buffer,Stencil Buffe ...

  7. mybatis使用*号查询数据丢失问题

    使用*号的SQL语句:select * from 表名 在开发过程中可能你会发现使用mybatis框架爱作为持久层时,写的SQL语句在工具里面查询数据是可以查到想要的数据的,但是在项目中会出现数据丢失 ...

  8. Hibernate 事务不回滚

    问题:               这几天在做开发时,发现事务不回滚了,Service是用AOP加的事务,数据库是MySql, 表全部是InnoDB:   方法回滚是采用spring的手动回滚:   ...

  9. Spring的绿草丛

    Spring 轻量级框架,JavaEE的春天,当前主流框架 “站立式”的企业应用开发框架 目标 实现有的技术更加易用,推进编码最佳实践 内容:loC容器,AOP实现,数据访问支持:简化JDBC/ORM ...

  10. vue调用豆瓣API加载图片403问题

    "豆瓣API是有请求次数限制的”,这会引发图片在加载的时候出现403问题,视图表现为“图片加载不出来”,控制台表现为报错403. 其实是豆瓣限制了图片的加载,我自己用了一个办法把图片缓存下来 ...