题目链接:

https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/

题目描述:

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1] Output:
[5,6]

题目翻译:
题目要求:给出一个整形数组,长度为n,数组中的整数都在1~n之间,有些数出现一次,有些数出现两次,求出那些没出现的数的集合。 解决方案:
1.

Swap the array, make the a[i] become the (a[i]-1)-th elements in the array.


Traverse the array, if a[i]!=i+1, add i+1 into the answer array.


参考:https://blog.csdn.net/zibingling777/article/details/79008604


class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> ans;
int n=nums.size();
for(int i=0;i<n;i++){
int j=i;
while(nums[j]!=nums[nums[j]-1]){
swap(nums[j],nums[nums[j]-1]);
}
}
for(int i=0;i<n;i++){
if(nums[i]!=i+1)
ans.push_back(i+1);    //向向量vector,ans后面依次退入元素。
}
return result;
}
};

2 .时间复杂度为O(n),空间复杂度为O(n)的做法。定义一个长度为n的数组temp,全都初始化为0。然后遍历给出的数组,令temp[nums[i]-1]=-1。

最后遍历数组temp,那些值为0的数的位置加上1就是所要求的没有出现的数字。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int l=nums.size();
vector<int> temp(l,);
vector<int> result;
for(int i=;i<l;i++){
temp[nums[i]-]=-;
}
for(int i=;i<l;i++){
if(temp[i]==)
result.push_back(i+);
}
return result;
}
};

3 .时间复杂度为O(n),空间复杂度为O(1)的做法。(符合题目要求的做法,没有使用多余空间)。遍历数组,索引为i,将nums[i]-1所在的位置的数置为负数。再次遍历数组,索引为index,对于每个数组值,如果为负数,就代表index+1这个数已经出现过了,如果为正数,代表没出现过,放到要返回的数组里面。

class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> result;
int n=nums.size();
for(int i=;i<n;i++){
int m=abs(nums[i])-; //去掉abs()就不能运行,为什么呢
nums[m]=nums[m]>?-nums[m]:nums[m]; //这是什么意思??
}
for(int i=;i<n;i++){
if(nums[i]>)
result.push_back(i+);
}
return result;
}
};

3. 思路

												

leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)的更多相关文章

  1. LeetCode: 448 Find All Numbers Disappeared in an Array(easy)

    题目: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice an ...

  2. LeetCode 448. Find All Numbers Disappeared in an Array (在数组中找到没有出现的数字)

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  3. LeetCode "448. Find All Numbers Disappeared in an Array"

    My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...

  4. 5. Leetcode 448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  5. LeetCode 448 Find All Numbers Disappeared in an Array 解题报告

    题目要求 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a ...

  6. LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

  7. [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字

    题目描述 给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些. 要求不能用额外的空间(除了返回列表之外),时间复杂度n 思路 因为不能用额外 ...

  8. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

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

随机推荐

  1. MSIL实用指南-装箱拆箱

    本篇讲述怎样装箱拆箱.装箱和拆箱都是针对值类型而言的,装箱的性能开销远比拆箱的性能开销大. 装箱装箱指令是Box.使用格式是 ILGenerator.Emit(OpCodes.Box,<值类型& ...

  2. [poj-2985]The k-th Largest Group_Treap+并查集

    The k-th Largest Group poj-2985 题目大意:给你n只猫,有两种操作:1.将两只猫所在的小组合并.2.查询小组数第k大的小组的猫数. 注释:1<=n,m<=20 ...

  3. ASP VNext 开源服务容错处理库Polly使用文档

    在进入SOA之后,我们的代码从本地方法调用变成了跨机器的通信.任何一个新技术的引入都会为我们解决特定的问题,都会带来一些新的问题.比如网络故障.依赖服务崩溃.超时.服务器内存与CPU等其它问题.正是因 ...

  4. sys用户密码丢失找回密码的步骤和命令

    假设你的sys用户密码丢失,写出找回密码的步骤和命令? 1.确认哪个数据库实例的sys用户密码丢失:(例:数据库实例为orclA) 2.进入数据库实例的目录中找到PWDorclA.ora文件:(例目录 ...

  5. iOS 消息发送与转发详解

    Objective-C 是一门动态语言,它将很多静态语言在编译和链接时期做的事情,放到了运行时来处理.之所以能具备这种特性,离不开 Runtime 这个库.Runtime 很好的解决了如何在运行时期找 ...

  6. 数据库(Oracle)运维工作内容及常用脚本命令

    1.系统资源状况:--内存及CPU资源  --linux,solaris,aix    vmstat 5  --说明:    1)观察空闲内存的数量多少,以及空闲内存量是否稳定,如果不稳定就得想办法来 ...

  7. spring框架学习笔记4:SpringAOP实现原理

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  8. Beta冲刺第六天

    一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:更新申请ip为域名,去除druid数据源统计 2.黄腾达:协作详情中添加成员对话框优化 3.张合胜:修复侧栏菜单mini状态下不能显示问题 三.明 ...

  9. 20162327WJH程序设计与数据结构第七周总结

    学号 20162327 <程序设计与数据结构>第7周学习总结 教材学习内容总结 1.关于接口的理解:接口可以理解为比较纯粹的抽象类 2.接口的特点:用interface定义接口 接口中的方 ...

  10. APP的案例分析-美团外卖

    大一才开始用软件订外卖了,很方便  ,上手快只要注册个账号登陆即可,支付时自动跳转到其他支付应用.严重的bug也没有,只有之前一段时间通过首单可以刷优惠,之后也修复了. 身边的同学也很多都在用.方便省 ...