• 问题描述

    • 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]
  • 测试用例:

    • 输入数组为空:[]
    • 输入数组长度为1:[1]
    • [4,3,2,7,8,2,3,1]
    • [1,2,3]
  • 思路分析:

    • 第一次:因为数组内所有元素都在[1,n]之间,其中n为数组长度。故而假设数组没有重复的数组,那么数组排序之后就会由[1,2,3,.....,n]此时a[i] = i+1,因此我们可任意遍历两次数组, 第一次遍历时,将数组元素放在它排序后的位置,使a[i] = i + 1, 之后再次遍历时一次为判断条件, 当数组内元素不满足该条件时,i+1就是所缺失的元素。此方法不需额外的存储空间,时间复杂度为遍历数组的时间O(n),满足要求

      class Solution {public List<Integer> findDisappearedNumbers(int[] nums) {List<Integer> list = new ArrayList<>();if(nums.length <= 1) return list;for(int i = 0; i < nums.length; i++){while(nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]){int temp = nums[i];nums[i] = nums[nums[i] - 1];nums[temp - 1] = temp;}}for(int i = 0; i < nums.length; i++){if(nums[i] != i + 1){list.add(i + 1);}}return list;}
      }

转载于:https://www.cnblogs.com/clairexxx/p/10531835.html

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

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

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

  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. C#LeetCode刷题之#448-找到所有数组中消失的数字(Find All Numbers Disappeared in an Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3712 访问. 给定一个范围在  1 ≤ a[i] ≤ n ( n ...

最新文章

  1. 判断dataset是否为空
  2. 导出对象_从代数几何到导出代数几何:形变与逼近
  3. 敏捷项目管理—敏捷四宣言
  4. UPS蓄电池的内阻没有参考值,如何才能发现劣化蓄电池?
  5. 微软发布正式版SQL Server 2016
  6. Java虚拟机(八)——堆
  7. Spark学习之第一个程序打包、提交任务到集群
  8. 定值保险计算举例_机动车辆保险的一些购买原则181536312
  9. 软件设计师考试大纲2018
  10. 营业执照、组织机构代码、统一社会信用代码
  11. 社团挖掘算法——BGLL算法
  12. 题目29 英文输入法单词联想
  13. 大数据:正在到来的数据革命
  14. 网络安全----身份认证
  15. 计算机中word繁体字转简体字,word简繁体怎么转换的两种方法
  16. 使用ViewPager和PhotoView实现图片浏览
  17. 使用PicGo快速搭建图床
  18. 2021暑假Leetcode刷题——Two Pointers(1)
  19. 【牛客】CPU的运算速度与许多因素有关,下面______是提高速度的有效措施?
  20. Win10自动更新永久关闭Windows update medic service拒绝访问

热门文章

  1. DUBBO与ZOOKEEPER、SPRINGMVC整合和使用
  2. 看完这篇文章还能不懂Flask这种Web框架吗?
  3. asyncio结合线程池
  4. Mybatis懒加载机制
  5. PHP遍历文件夹下所有文件
  6. C库中没有itoa以及C++中string没有split方法的原因
  7. C编译: 使用gdb调试
  8. CISCO无线AP配置手册
  9. 笔记本电脑如何强制关机_如果你的MacBook一直关机,该怎么办?
  10. Redis主从复制、哨兵模式和分布式集群