Leetcode  448. Find All Numbers Disappeared in an Array

Add to List

Description Submission Solutions

  • Total Accepted: 31266
  • Total Submissions: 58997
  • Difficulty: Easy
  • Contributors: yuhaowang001

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]

Subscribe to see which companies asked this question.

题解:

(1), 不能申请额外的空间, 只能在原数组进行swap操作了。

(2), 根据数据的类型,大小都是处于 [1, n], 进行位置操作。

class Solution {
public:vector<int> findDisappearedNumbers(vector<int>& nums) {vector<int> ans;int i = 0; while(i<nums.size()){if(nums[i] != nums[ nums[i]-1 ]){swap(nums[i], nums[ nums[i]-1 ] ); }else{++i; }}for(int i=0; i<nums.size(); ++i){if(nums[i] != i+1){ans.push_back( i+1 ); }}return ans; }
};

  

转载于:https://www.cnblogs.com/zhang-yd/p/6528014.html

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

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

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

  5. LeetCode之Find All Numbers Disappeared in an Array

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

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

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

最新文章

  1. Windows 曝漏洞 —— 影响所有版本
  2. 准备重新回归信息安全产业
  3. Spring事务传播属性和隔离级别
  4. 华为p20pro投屏到笔记本_新荣耀笔记本与微软系统合作,网友:一碰即传投屏功能还有吗...
  5. 开启Github之旅
  6. pdf转换成ppt转换器免费版
  7. ENVI53 辐射校正、大气校正、影像裁剪超详细教程
  8. 苹果库乐队怎么玩_苹果手机使用技巧(例iPhone11)
  9. 二手车数据挖掘- 数据分析
  10. CSAPP Bomb Lab
  11. ᑋᵉᑊᑊᵒ ᵕ̈ᰔᩚ
  12. POI单元格合并(合并后边框空白修复)、自动列宽、水平居中、垂直居中、设置背景颜色、设置字体等常见问题
  13. 题目内容: 你的程序要读入一个整数,范围是[-100000,100000]。然后,用汉语拼音将这个整数的每一位输出出来。 如输入1234,则输出: yi er san si 注意,每个字的拼音
  14. VISUAL SVN安装 及客户端使用
  15. mixly for linux64,Mixly编程for Linux
  16. mysql数据库备份工具
  17. 锁相环的组成和原理及应用
  18. AXI总线学习(AXI34)
  19. Android游戏框架AndEngine使用入门
  20. mysql主从三个线程工作顺序_MySQL主从介绍、准备工作、配置主、配置从、测试主从同步...

热门文章

  1. 卸载SD卡对MediaServer的处理
  2. Objective-C学习总结-13协议1
  3. 软件配置管理(CN) Google讨论组
  4. 蓝桥杯 ADV-157算法提高 现代诗如蚯蚓
  5. 服务器_Windows Server 2012 服务器之Web服务器
  6. python运维脚本部署jdk_Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
  7. java 将 ResultSet 转化为 json格式
  8. 设计模式之—工厂方法模式
  9. BitCoin Gloom系列
  10. linux-0.11 内核源码学习笔记一(嵌入式汇编语法及使用)