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个数,其中1~n中有些数没出现,求这些没出现的数。

我们可以将数值和数组的标关联,运用负号巧妙的解决了这个问题。确保每个出现过的数都变成负数

class Solution {
public:vector<int> findDisappearedNumbers(vector<int>& nums) {vector<int> v;for (int i = 0; i < nums.size(); ++i) {int index = abs(nums[i]) - 1;nums[index] = -abs(nums[index]);}for (int i = 0; i < nums.size(); ++i) {if (nums[i] > 0) v.push_back(i + 1);}return v;}
};

转载于:https://www.cnblogs.com/pk28/p/7250740.html

448. Find All Numbers Disappeared in an Array相关推荐

  1. LeetCode 448. Find All Numbers Disappeared in an Array 442. Find All Duplicates in an Array

    这两道题很有意思,由于元素为1~n,因此每个元素的值-1(映射到0~n-1)就可以直接当做下标.这样将 nums 中对应下标的元素 *-1 以i表示 index+1 这个元素出现过了,能节省存储的空间 ...

  2. Leetcode 448. Find All Numbers Disappeared in an Array

    Leetcode  448. Find All Numbers Disappeared in an Array Add to List Description Submission Solutions ...

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

  4. 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数

    [抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...

  5. [swift] 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 ...

  6. 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 | 448. 找到所有数组中消失的数字(原地,位运算)

    题目 https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题解 遍历数组,将数组中每个数字 n 作为下标,将 ...

  8. 448. Find All Numbers Disappeared in an Array645. Set Mismatch

    题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...

  9. Find All Numbers Disappeared in an Array

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

最新文章

  1. ThickBox 3.1参数详解(转)
  2. Hyper-v 3.0虚拟化平台群集共享磁盘无法failover的故障
  3. 什么是安全测试?哪些阶段需要安全测试?
  4. 计算机网络的定义分类性能指标,第1章 计算机网络基础
  5. 爬取某东600多本书籍,用数据帮你分析哪些Python书籍值得选择(上)
  6. 制作linux usb安裝,如何在Linux上安装和使用Etcher来制作Linux临场USB
  7. webkit内核浏览器的CSS写法
  8. 2018-04-26java实习面试记录
  9. centos rpm安装mysql5.6_CentOS 7下使用RPM安装MySQL 5.6
  10. DAS Tool 介绍
  11. springboot配置微信公众号获取openid
  12. 计算机制图基本知识6,机械制图及计算机绘图教案.doc
  13. java程序员电脑内存配置_学习JAVA对电脑配置有要求吗
  14. RAID磁盘阵列数据恢复
  15. 全球及中国智能手机过滤器行业销售动态及投资盈利预测报告(2022-2027)
  16. PhoneApplicationFrame以及设置Obscured/Unobscured的event handler
  17. 【统计学笔记】如何判断变量间相关关系,并建立一元线性回归模型?
  18. ¥1-3 SWUST oj 942: 逆置顺序表
  19. c语言字符串输入的陷阱
  20. 网络资源下载方式:http/https、ftp/sftp、BT种子、磁力下载、ed2k下载等的区别

热门文章

  1. cufon,在网页上画出特殊字体
  2. [原创]我的作品:我的迷宫小游戏Java版本
  3. Mac OS X snow leopard 10.6 VMware安装
  4. 通过Image 对象动态装载图片,不可能立即得到图片的问题
  5. 做好MSSQL保卫战之xp_cmdshell
  6. FlinkShell用kill -9杀不掉
  7. BootStrap轮播图失效
  8. spark中用println输出一些控制信息
  9. redis删除指定key中的指定元素
  10. ubuntu16.04下面的redis desktop manger的使用