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:

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

[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. java_过滤器

    /** 过滤器: * File类中有两个和listFiles方法重载的方法,方法的参数就是过滤器 * listFiles(FileFilter filter) * java.io.FileFilter ...

  2. Python全栈开发:socket代码实例

    客户端与服务端交互的基本流程 服务端server #!/usr/bin/env python # -*- coding;utf-8 -*- import socket sk = socket.sock ...

  3. 类欧几里德算法(洛谷 P5170

    #include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...

  4. python类的静态方法和类方法区别

    先看语法,python 类语法中有三种方法,实例方法,静态方法,类方法. # coding:utf-8 class Foo(object): """类三种方法语法形式&q ...

  5. odoo widgets.js 笔记

    // 在OpenERP的Web框架内, // 通过声明一个函数来声明一个JavaScript模块[openerp.ext_picking就是这个JS模块], // 并把这个函数放在全局变量opener ...

  6. php链表笔记:链表的检测

    <?php /** * Created by PhpStorm. * User: huizhou * Date: 2018/12/2 * Time: 11:48 */ /** * 链表的检测 * ...

  7. PAT甲级——A1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  8. centos7服务器常见安装包准备

    内核相关配置 https://github.com/digoal/blog/blob/master/201611/20161121_01.md# vi /etc/sysctl.conf # add b ...

  9. Python中的urlparse、urllib抓取和解析网页(一)

    对搜索引擎.文件索引.文档转换.数据检索.站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理.事实上,通过Python 语言提供的各种模块,我们无需借助Web服务器或者Web浏览 ...

  10. 关于Canvas的坐标系

    注意Canvas的坐标系应该是这样子的: 看下面的例子: 最后的显示效果是: