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]

思路:把数组nums中的每一个数所对应的索引的位置的数置为负数。

代码:class Solution {

public:

    vector<int> findDisappearedNumbers(vector<int>& nums) {

        vector <int> result;

        for(auto n:nums)

            nums[abs(n)-1]=-abs(nums[abs(n)-1]);

        for(int i=0;i<nums.size();i++)

            if(nums[i]>0)

                result.push_back(i+1);

        return result;

       

    }

};

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

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

  2. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

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

  3. LeetCode "448. Find All Numbers Disappeared in an Array"

    My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...

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

  5. LeetCode: 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 an ...

  6. LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

  7. [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字

    题目描述 给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些. 要求不能用额外的空间(除了返回列表之外),时间复杂度n 思路 因为不能用额外 ...

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

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

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

随机推荐

  1. 短信发送接口被恶意访问的网络攻击事件(三)定位恶意IP的日志分析脚本

    前言 承接前文<短信发送接口被恶意访问的网络攻击事件(二)肉搏战-阻止恶意请求>,文中有讲到一个定位非法IP的shell脚本,现在就来公布一下吧,并没有什么技术难度,只是当时花了些时间去写 ...

  2. 门(door)

    门(door) 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 第一行是一个正整数n,表示原始字符串的长度.第二行是一个字符串,长度为n.字符串由大小写字母,数字,符号,空格构成. ...

  3. 并发编程(一):从头到脚解读synchronized

    一.目录 1.多线程启动方式 2.synchronized的基本用法 3.深度解析synchronized 4.同步方法与非同步方法是否能同时调用? 5.同步锁是否可重入(可重入锁)? 6.异常是否会 ...

  4. EntityFramework连接SQLite

    EF很强大,可惜对于SQLite不支持CodeFirst模式(需要提前先设计好数据库表结构),不过对SQLite的数据操作还是很好用的. 先用SQLiteManager随便创建一个数据库和一张表:

  5. 生成一个唯一token

    $token = md5(uniqid(rand(), true));

  6. 文件存储B+树

    文件存储要选用B+树这样的数据结构 “文件存储要选用B+树这样的数据结构”——没记错的话,这是严蔚敏那本数据结构书上的一句结论.不知道是我没细看还是她没细讲,反正当时纯粹应试地记了这么个结论.不求甚解 ...

  7. Administrator账户密码忘了怎么办

    WIN7的Administrator账户密码忘了怎么办   1.准备一个带有winpe系统的系统光盘或者U盘 2.启动电脑,按F12选择U盘或者光盘启动,进入winpe操作系统 3.找到c:\wind ...

  8. Kanzi 倒影效果制作

    在kanzi中,倒影效果会经常用到,比如多媒体中. 先来看一下最终的实现效果: 在这个效果中,我们的需求是,倒影图与原图一致,透明度和可见范围可以调节. 下面说一下实现的步骤: 1.创建工程后,Roo ...

  9. tomcat 修改为自己项目界面

    修改Tomcat欢迎界面为自己项目界面 Posted on 2011-04-16 13:10 IceWee 阅读(1062) 评论(0)  编辑  收藏 所属分类: Tomcat  由于项目要发布到互 ...

  10. 一个Ruby静态代码分析器 rubocop

    A Ruby static code analyzer, based on the community Ruby style guide. http://rubocop.readthedocs.io ...