描述

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]

分析

这道题要是先排序,再计算,就会十分简单,然而平均性能最好的快排也只有O(nlgn),明显不符合题目要求。

一开始想了挺久都没解出来,后来看了讨论区 https://discuss.leetcode.com/topic/65944/c-solution-o-1-space

的答案才知道的。

这种解法还是挺巧妙的,我怎么就想不到呢。。。还是做的题目太少了啊。。

解法如下

由于数组大小是n,且元素值范围为[1,n],因此可以从下标和元素值大小的联系入手。

遍历该数组,对于每一个元素,都将对应的元素置为负数,如对于题目所给数组,第一个元素为4,则将第四个元素7(对应的数组的下标为3)置为负数,如果是负数,则不作处理。遍历结束后,数组元素 变为:

[-4,-3,-2,-7,8,2,-3,-1]

第5个元素8需要下标为4的元素来将其置为负数,由于数组中没有5这个元素,因此该元素值仍为正数8,同理,第6个元素(对应的下标为5)也是正数。

因此,只需要再遍历一次数组,每遇到一个正数,就说明对应的元素不在数组中,将该元素压入vector中,遍历结束后,即可得到结果。

代码如下:

class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
if(nums.size() == 0)return nums;
vector<int>ret;
for(int i = 0; i != nums.size(); ++i){
int m = abs(nums[i]) - 1;
nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
}
for(int i = 0; i != nums.size(); ++i){
if(nums[i] > 0)ret.push_back(i + 1);
}
return ret;
}
};

leetcode解题报告(33): Find All Numbers Disappeared in an Array的更多相关文章

  1. [LeetCode&Python] Problem 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 ...

  2. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  3. LeetCode——Find All Numbers Disappeared in an Array

    LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...

  4. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  6. leetcode之Find All Numbers Disappeared in an Array

    问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到 ...

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

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

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

  9. LeetCode_448. Find All Numbers Disappeared in an Array

    448. Find All Numbers Disappeared in an Array Easy Given an array of integers where 1 ≤ a[i] ≤ n (n  ...

  10. 448. Find All Numbers Disappeared in an Array&&645. Set Mismatch

    题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...

随机推荐

  1. Codeforces Round #570 Div. 3

    A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...

  2. Linux进程自保护攻防对抗技术研究(Process Kill Technology && Process Protection Against In Linux)

    0. 引言 0x1: Linux系统攻防思想 在linux下进行"进程kill"和"进程保护"的总体思路有以下几个,我们围绕这几个核心思想展开进行研究 . 直接 ...

  3. Apach Shiro MD5密码加密过程(明文生成密码过程)详细解析

    前言: 最近再项目当中使用的ApachShiro安全框架,对于权限和服务器资源的保护都有一个很好的管理.前期主要参考的文章有 项目中设计密码的加盐处理以及二次加密问题,跟着断点 一步步揭开Apach ...

  4. 10 查询字符串,X字段必须包含(不包含)XX;_all原理

    指定某个字段,必须要包含XX字符 GET /beauties/my/_search?q=Name:Chang Wei   搜出 某个字段不包含XX字符 的所有内容 GET /beauties/my/_ ...

  5. PKPM BIMViewer的使用

    模型的使用,目前有两个方案, 一个是使用全局组件,在单页面的主页面中进行嵌套 <template> <div id="model"> <!-- 这样的 ...

  6. Math对象的一些方法

    ceil(n) 返回n向上取整的最近的整数floor(n) 返回n向下取整到最近的整数max(a,b,c...) 返回最大值min(a,b,c...) 返回最小值round(n) 返回n四舍五入的最近 ...

  7. 浮动和渐变色,定位position,元素的层叠顺序

    浮动: float 是我们网页布局的一种 浮动 可以有 left 左浮动 right 右浮动 两种 浮动的特点: 脱离正常的文档流,原本的空间不占据,浮动的标签都具有块级标签的一些特点,可以手动设置宽 ...

  8. python day6 装饰器补充,正则表达式

    目录 python day 6 1. 装饰器decorator 2. 正则表达式re 2.1 正则表达式概述 2.2 re模块常用方法 python day 6 2019/10/09 学习资料来自老男 ...

  9. Part_three:Redis持久化存储

    redis持久化存储 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. 1.RD ...

  10. css小技巧 --> 单标签实现单行文字居中,多行文字居左

    可能出现的尺寸场景: 代码如下: <!DOCTYPE html> <html lang="zh"> <head> <meta charse ...