题目链接:

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. linux挂载windows共享文件夹

    1.建立共享文件夹 2.在linux中挂载共享目录 #mount -t cifs -o username=administrator,password=你的系统账号密码 //192.168.0.22/ ...

  2. css3 背景色 实现边框渐变运动动画

    css3 #body_id { animation: myfirst 10s ease-in-out -2s infinite alternate; /* Firefox: */ -moz-anima ...

  3. java基础(5)----面向对象

    编程思想: 简单的说一下,我们学习编程,最重要的就是要有编程思想,而编程思想无非就是面向过程和面向对象,以下谈谈我对编程思想的理解. 面向过程: 从过程入手,第一步,第二步--.借助过程与过程的配合, ...

  4. 新事物学习---Chrome上使用PWA

    PWA是什么 PWA(Progressive Web Apps)是 Google 最近在提的一种 Web App 形态 (或者如 Wikipedia 所称的"软件开发方法").PW ...

  5. alpha-咸鱼冲刺day5-紫仪

    总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 !!!QAQ可以做到跟数据库交互了!!!!先来撒花花!(然后继续甲板) 四,问题困难   日常啥都不会,百度真心玩一年 ...

  6. NOIP2016 天天爱跑步 80分暴力

    https://www.luogu.org/problem/show?pid=1600 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养 ...

  7. redux的知识点

    Redux: Redux 是针对 JavaScript应用的可预测状态容器 就是用来管理数据的.stroe 保存数据action领导 下达命令reducer员工 执行命令 下载命令:  npm ins ...

  8. 新特性GTID

    什么是GTID 每提交一个事务,当前的执行过程都会拿到一个唯一的标识符,此标识符不仅对其源mysql 实列是唯一的而在给定的复制环境中的所有mysql 实列也是唯一的,所哟的事务与其GTID 之间都是 ...

  9. Python内置函数(8)——bool

    英文文档: class bool([x]) Return a Boolean value, i.e. one of True or False. x is converted using the st ...

  10. jmeter入门(02)测试报告各项指标含义

    一.名词定义(时间单位ms) 1.聚合报告 Sample:本次测试场景共运行多少个请求: Average:平均响应时间: Median:统计意义上的响应时间中值: 90% line:所有线程中90%的 ...