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 ...
随机推荐
- Servlet规范简介
引言 Web 框架一般是通过一个 Servlet 提供统一的请求入口,将指定的资源映射到这个 servlet, 在这个 servlet 中进行框架的初始化配置,访问 Web 页面中的数据,进行逻辑处理 ...
- 「Baltic2015」Network
题目描述 原文 The government of Byteland has decided that it is time to connect their little country to th ...
- [BZOJ4538]网络
今天打比赛,毒瘤yww把这题出到$n,m\leq 5\times10^5$,因为不会写整体二分所以来写坑爹的$O\left(n\log_2n\right)$做法 考虑按重要度建权值线段树(相同权值的请 ...
- 【可持久化Trie】bzoj3261 最大异或和
对原序列取前缀异或值,变成pre[1...N],然后询问等价于求max{a[N]^x^pre[i]}(l-1<=i<=r-1). #include<cstdio> #defin ...
- 关于SQL优化方面的一些总结
在sql查询中为了提高查询效率,我们常常会采取一些措施对查询语句进行sql优化,下面总结的一些方法,有需要的可以参考参考. 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ord ...
- 在sublime执行自定义脚本
[背景] 一般项目都会有一个预处理的脚本, 在发布,或者预览效果的时候,往往要先执行脚本. 想法来自editplus 习惯了editplus的同学,都知道,可以配置自定义执行的脚本. 一般我会把它配置 ...
- YUV12(420) (from)to RGB24
直接上代码 #include <opencv2/opencv.hpp> #include <stdio.h> #define min(a,b) ((a<b)?a:b) # ...
- http://zhidao.baidu.com/link?url=inouJq69pK4PVM2L26fvnxXfRKhJ1uKmttgVqIEqld14SEUa8JzXZfRYHS3qdltqMXBgEQycFsF8AI9DlSoH4_
http://zhidao.baidu.com/link?url=inouJq69pK4PVM2L26fvnxXfRKhJ1uKmttgVqIEqld14SEUa8JzXZfRYHS3qdltqMXB ...
- Docker解析及轻量级PaaS平台演练(三)--Dockerfile编写
在本篇中将介绍Dockerfile的编写 除了通过修改Image,创建Container,在打包成Image来创建我们需要的Image之外 我们还可以编写Dockerfile文件,通过build来创建 ...
- mysql增量备份(2/2)
前言 这是在百度文库里看到的文章,原名叫做<MYSQL 完全与增量备份及恢复文档 >,是关于完全备份和增量备份以及恢复文档的...... 文档介绍 本文档采用 mysqldump 对数据 ...