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] 思路:给出长度为n的数组,遍历所有元素,找出没有出现过的元素。基本思想是我们将元素值作为下标遍历输入数组并标记为负值num[i] = -num[i]。
vector<int> finddisappear(vector<int>& nums){
vector<int>tmp;
size_t n = nums.size();
for(size_t i = ; i < n; i++){
int val = abs(nums[i]) - ;//nums[i]是所有元素值,val是下标,将元素值出现的下标的元素标记为负值,减1的意思是下标比个数少1
if(nums[val] > )
nums[val] = -nums[val];
} for(size_t i = ; i < n; i++){
if(nums[i] > )
tmp.push_back(i + );
}
return tmp;
}
比如4个元素的数组里出现了2,3,那么下标为2,3的元素标为负值,剩下的1,4没有出现,那么仍为正值,找到正值的下标也就是结果了。

[Array]448. Find All Numbers Disappeared in an Array的更多相关文章

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

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

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

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

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

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

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

  7. 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. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

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

随机推荐

  1. HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章

    原文:HTML5印章绘制电子签章图片,中文英文椭圆章.中文英文椭圆印章 电子签章图片采集 印章图片的采集两种互补方式: 方式1:在线生成印章图片方式,但是这种方式有个弊端,对印章中公司名称字数有限制, ...

  2. string json list

    String str="[{\"cIndex\":14,\"column\":\"nextAdvice\",\"id\& ...

  3. Android开发 获取View的尺寸的2个方法

    前言 总所周知,在activity启动的onCreate或者其他生命周期里去获取View的尺寸是错误的,因为很有可能View并没有初始化测量绘制完成.你这个时候获取的宽或的高不出意外就是0.所以,我们 ...

  4. Linux定时重启

      1.安装crontabyum install cixie-cron  yum install crontabs    2.编辑cron第一步,登陆账号第二步,输入crontab -e第三步,输入i ...

  5. jvisualvm图解【转】

    jvisualvm图解[转]   http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收, ...

  6. 2016.9.15初中部上午NOIP普及组比赛总结

    2016.9.15初中部上午NOIP普及组比赛总结 2016.09.15[初中部 NOIP普及组 ]模拟赛 又翻车了!表示时超和空超很可恨! 进度 比赛:AC+0+0+20=120 改题:AC+80+ ...

  7. ArrayList去除重复元素(多种方法实现)

    package other; import java.util.ArrayList; import java.util.HashSet; public class test4 { public sta ...

  8. MyEclipse如何使用debug模式

    知道如何打断点,如何进入debug与debug模式的视图,还有工具栏的使用和快捷键的使用 https://blog.csdn.net/menglanyingfei/article/details/55 ...

  9. 最后的egret

    坚持做一件事真的好难~ 决定重新写博客的时候想着一定要坚持一个周一篇,然而.... 年后上班老板找我的第一件大事:以后公司的棋牌产品不会有大的动作了:公司PHP(内部用的运营后台)的小姐姐休产假了,我 ...

  10. PAT甲级——A1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...