LeetCode——Find All Numbers Disappeared in an Array

Question

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]

Solution

用hash table求解。

Answer

class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
map<int, int> dict;
for (int i : nums)
dict[i]++;
vector<int> res;
for (int i = 1; i <= nums.size(); i++)
if (dict[i] == 0)
res.push_back(i);
return res;
}
};

时间复杂度O(2n)。 网上给出了另外一种解答:

The idea is very similar to problem 442. Find All Duplicates in an Array: https://leetcode.com/problems/find-all-duplicates-in-an-array/.

First iteration to negate values at position whose equal to values appear in array. Second iteration to collect all position whose value is positive, which are the missing values. Complexity is O(n) Time and O(1) space.

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

它的意思是数组中有这个数,那么对应位置的数就会变成负的,如果数组中缺失某个数,那么这个数对应位置的数最后就是正的。所以,凡是最后为正的数的位置都是缺失的数。

LeetCode——Find All Numbers Disappeared in an Array的更多相关文章

  1. LeetCode Find All Numbers Disappeared in an Array

    原题链接在这里:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题目: Given an array o ...

  2. [LeetCode] 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之Find All Numbers Disappeared in an Array

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

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

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

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

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

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

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

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

  9. 448. Find All Numbers Disappeared in an Array@python

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

随机推荐

  1. A1231. Crash的数字表格(贾志鹏)

    A1231. Crash的数字表格(贾志鹏) 时间限制:2.0s   内存限制:512.0MB   总提交次数:410   AC次数:154   平均分:63.93   将本题分享到:        ...

  2. 【BZOJ3875】[Ahoi2014&Jsoi2014]骑士游戏 SPFA优化DP

    [BZOJ3875][Ahoi2014&Jsoi2014]骑士游戏 Description  [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会扮演一个英勇的 ...

  3. 支付宝app支付提示 系统繁忙,请稍后重试

    v2版本的支付宝支付,无论怎么调试返回的都是系统繁忙,请稍后重试提示.经过对给的示例代码的仔细研究,最后发现是由于老版本的sign_type 不加入签名计算,而新版本的支付签名它是加入签名计算的.供大 ...

  4. PAT 1016 Phone Bills(模拟)

    1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...

  5. Java——BeanUtils基本用法

    为了操作JavaBean的属性,sun公司自己写了一套内省的api(在Java.beans.*)中,但是我们发现操作起来还是比较复杂的,所以apache公司就自己写了一套api替代了它,大大方便了开发 ...

  6. pt-online-schema-change线上DDL注意事项

    在使用pt-online-schema-change做线上DDL的时候对于数据量比较小的表问题不大,但是对于数据量比较大的表(比如:单表2亿数据)访问量较大的时候就有可能出现异常, 原本使用pt-on ...

  7. JS toLowerCase()方法 toUpperCase()方法

    toLowerCase()方法: 定义:toLowerCase() 方法用于把字符串转换为小写. 语法:var str = "String"; str .toLowerCase() ...

  8. Java基础 - 字符串 String

    字符串就是用字符拼接成的文本值,字符串在存储上类似数组,在java语言中把字符串当做对象进行处理 创建字符串 package com.mingri.chapter_02; public class d ...

  9. Linux上添加服务(htttpd,samba等等)

    service httpd restart   出现 httpd:unrecognized service 错误 vi /etc/rc.d/rc.local #在/etc/rc.d/rc.local中 ...

  10. 原!mysql5.6 存储过程 批量建表

    由于业务需求,需要按天分表,因此写了个存储过程,根据时间生成表. 根据createTime 的时间,以及 while循环的变量设置范围,生成该指定日期及之后的多张表. BEGIN ); ); ; '; ...