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 = 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]
解法一:
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int len = nums.size();
for(int i=; i<len; i++) {
int m = abs(nums[i])-; // index start from 0
nums[m] = nums[m]> ? -nums[m] : nums[m];
}
vector<int> res;
for(int i = ; i<len; i++) {
if(nums[i] > ) res.push_back(i+);
}
return res;
}
};
参考了大神的解法和解释: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.The basic idea here is to label all appeared numbers in the array. Since we don't want to introduce extra space and given all numbers are positive(from 1 to n), negate the value in the corresponding position is one choice. Ex, for input like [1, 2, 2], after the first sweep, it becomes [-1, -2, 2]. At position index=2, there is a positive value, which means it corresponding value 3 is not showing.
Hope this simple example gives you some lead :-)
假如我们给定的序列为[8,7,1,2,8,3,4,2],下面给出推导:

每行标记颜色的表示该次改变的值,黄色为第一次搞,即需要变为负值;绿色为非第一次搞,已经为负值了,就不要改变了。最后可以看到剩下为正数的下标就是我们的所求。
再解释一下为什么必须要abs(nums[i]),是因为题目中数组的长度就是n,而且最大值也是n为止,那么既少元素又要凑够n个数,里面必然是有重复的元素的,那么对于重复的元素我我们一开始置为了负值,如果不取abs的话,下次再来就会造成数组越界的。比如下面就是越界的例子:

解法二:
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
int n = nums.length;
for (int i = ; i < nums.length; i ++)
{
nums[(nums[i]-) % n] += n;
}
for (int i = ; i < nums.length; i ++)
{
if (nums[i] <= n) res.add(i+);
}
return res;
}
}
另外一位大神的解法:We can change back the value after find out result;How to do it?We can simply traverse array again and for each element, call nums[i] = nums[i] % n; to restore.
448. Find All Numbers Disappeared in an Array【easy】的更多相关文章
- 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 = ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- 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 ...
- 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 ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 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 ...
- 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 ...
随机推荐
- 8.1(java学习笔记)注解(Annotation)
一.Annotation Annotation不是程序本身,但它可以对程序进行解释,这一点和注释类似. 但最大的不同点在于,注解可以被其他程序读取,然后可以对其进行一些有针对性操作. 这是注解与注释最 ...
- 5.7(java学习笔记)Vector、Enumeration
一.Vector Vector类实现一个可扩展的数组对象.与数组一样,它包含可以使用整数索引访问. 它的基本操作方法add(int index, E element),get(int index),i ...
- 【R笔记】R语言利器之ddply
ddply()函数位于plyr包,用于对data.frame进行分组统计,与tapply有些类似 准备数据 # 使用stringsAsFactors=F来防止data.frame把向量转为factor ...
- scope的范围
(一)scope=“singleton” 知识点:无论获取多少个bean,得到的总是一样的地址,singleton范围下只会创建一个bean实例 1.Bean4.java package com.in ...
- CentOS--在CentOS安装PHP5.6
查看centos的版本: [root@localhost ~]# lsb_release -a LSB Version: :core-3.1-amd64:core-3.1-ia32:core-3 ...
- ES6笔记之参数默认值(译)
原文链接:http://dmitrysoshnikov.com/ 原文作者:Dmitry Soshnikov 译者做了少量补充.这样的的文字是译者加的,可以选择忽略. 作者微博:@Bosn 在这个简短 ...
- mongodb聚合(转)
聚合 是泛指各种可以处理批量记录并返回计算结果的操作.MongoDB提供了丰富的聚合操作,用于对数据集执行计算操作.在 mongod 实例上执行聚合操作可以大大简化应用的代码,并降低对资源的消耗. 聚 ...
- Yii2-核心框架代码规范
1.概述 简单说,我们使用PSR-2兼容规范,所以应用于PSR-2的一切对我们的代码也同样适用. 文件必须使用 <?php 或 <?= 标签. 文件未尾应该有一个新行. PHP代码文件必须 ...
- SSH 错误解决案例1:Read from socket failed: Connection reset by peer
今天早上天天连接的开发机突然报出连接错误. 这个错误是SSH最常见错误,造成的原因也是千奇百怪(具体可goole),下面描述我的server的问题: 客户端报错 [root@server]# ssh ...
- SpringMVC中使用CommonsMultipartResolver进行文件上传
概述: CommonsMultipartResolver是基于Apache的Commons FileUpload来实现文件上传功能的.所以在项目中需要相应的jar文件. FileUpload版本要求1 ...