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】的更多相关文章

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

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

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

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

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

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  7. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

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

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

随机推荐

  1. 【floyd】CODEVS 1077 多源最短路

    floyd模板 #include<cstdio> #include<algorithm> using namespace std; ][],m,x,y,n; int main( ...

  2. iOS 公司开发者账号申请清单

    公司开发者账号申请清单: Apple ID账号申请: (有账号请提供账号密码) Apple ID:       (最好是公司邮箱账号) Apple ID密码:  (大于8位, 字母或数字组成,  包含 ...

  3. 再谈EditText只能输入金额

    上次写了一篇EditText只能输入金额的博客,后来发现一个bug,当还未输入数字的情况下输入小数点程序就崩了,我去测了一下支付宝,看看会怎么样,我先输入小数点,程序正常,我再输入数字,可以正常输入, ...

  4. SQL Server Wait Types Library

    https://www.sqlskills.com/blogs/paul/announcing-the-comprehensive-sql-server-wait-types-and-latch-cl ...

  5. 大湿教我写程序(2)之走向AV之路

    一.大摆庆功宴 上一篇博文<大湿教我写程序(1)之菜单导航篇>中讲到了我撸码到晚上两点多,整出了一个还算是高端大气上档次的demo.半夜回到家里打算着可以好好睡上一个懒觉,到时候直接到客户 ...

  6. asp.mvc展示model

    1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model 主要负责维持数据状态,将数据从数据存 ...

  7. Java集合之保持compareTo和equals同步

    在Java中我们常使用Comparable接口来实现排序,其中compareTo是实现该接口方法.我们知道compareTo返回0表示两个对象相等,返回正数表示大于,返回负数表示小于.同时我们也知道e ...

  8. javascript快速入门15--节点

    节点类型 DOM定义了Node的接口以及许多种节点类型来表示节点的多个方面! Document——最顶层的节点,所有的其他节点都是附属于它的. DocumentType——DTD引用(使用<!D ...

  9. thread::id

    线程标识符id可以通过thread::get_id()获得,若thread obejct没有和任何线程关联则返回一个NULL的std::thread::id表示没有任何线程.当前线程若想获得自己的id ...

  10. 【BIEE】16_饼图应用

    在BIEE中,我们可以使用饼图来展示报表数据 饼图在使用中有三元素:①切片 ②饼图 ③度量 那么我们来分别看下这三个元素的功能分别是什么? 我们通过上图可以看出度量中存在2个度量,那么此时的饼图数量是 ...