描述

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. 关于PATCH与PUT的区别

    两者的区别:PATCH:更新部分资源,非幂等,非安全PUT:更新整个资源,具有幂等性,非安全注:幂等性:多次请求的结果和请求一次的结果一样安全性:请求不改变资源状态 举个两者明显区别的例子(我对两者定 ...

  2. 翻译 API

    Request http://fy.iciba.com/ajax.php?a=fy&f=auto&t=auto&w=love Pre 英译汉 Request http://fy ...

  3. python 基础(实例1——登陆小游戏)

    一个简单的登陆小游戏,输入用户名和密码,如果和user_passwd.txt中内容匹配,则打印“welcome to login...”,如果三次输入错误则将该用户列入黑名单,无法再用该用户名登陆. ...

  4. mysql的安装,启动,和基础配置 -----windows版本

    下载: 第一步 : 打开网址(进入官网下载) : https://www.mysql.com , 点击downloads之后跳转到https://www.mysql.com/downloads 第二步 ...

  5. 离线安装zabbix文档

    为了离线安装需要离线安装包,可以通过这个方式获取. 用yum安装软件默认不保存软件包,要保存需修改配置文件 #  vi   /etc/yum.conf 将keepcache的值改为1 安装版本:rel ...

  6. 自定义一个简单的JDBC连接池

    一.什么是JDBC连接池? 在传统的JDBC连接中,每次获得一个Connection连接都需要加载通过一些繁杂的代码去获取,例如以下代码: public static Connection getCo ...

  7. 尚硅谷MySQL基础学习笔记

    目录 写在前面 MySQL引入 数据库的好处 数据库的相关概念 数据库存储数据的特点 MySQL服务的启动和停止 MySQL服务端的登录和退出 MySQL的常用命令 MySQL语法规范 DQL(Dat ...

  8. MySQL Hardware--RAID卡BBU Learn Cycle

    RAID卡缓存策略 不同的RAID卡缓存策略对IO的性能影响较大,常见的策略有: 1.写操作策略,可设置为WriteBack或WriteThrough WriteBack:进行写操作时,将数据写入RA ...

  9. Linux命令——du

    参考:10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories 前言 du(Disk Usage), ...

  10. 使用Cloudera Manager部署oozie

    使用Cloudera Manager部署oozie 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 1>.进入CM服务安装向导 2>.选择要添加的oozie服务 3> ...