1、题目

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]
Input:
[0]
Output:
[1]

2、代码实现

public class Solution {public List<Integer> findDisappearedNumbers(int[] nums) {List<Integer> list = new ArrayList<Integer>();if (nums == null || nums.length == 0)return list;Map<Integer, Integer> map = new HashMap<Integer, Integer>();for (int  i = 0;  i < nums.length; ++i) {map.put(nums[i], 2);}for (int i = 1; i <= nums.length; ++i) {Integer in = map.get(i);if (in == null) { list.add(i);}}return list;}
}

3、总结

当我们需要找到数组集合里面没有包含哪个元素的时候,我们可以采用HashMap来解决这个问题
比如{1, 2,4, 2, 5}
还有就是找2个字符串里面第一个不重复元素的问题,我们可以采用HashMap来解决这个问题

s = "leetcode"
return 0.
s = "loveleetcode",
return 2.

或者找字符数组里面唯一一个重复的字符都可以采用HashMap来解决这个问题

LeetCode之Find All Numbers Disappeared in an Array相关推荐

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

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

  2. 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 这个元素出现过了,能节省存储的空间 ...

  3. leetcode 448. Find All Numbers Disappeared in an Array | 448. 找到所有数组中消失的数字(原地,位运算)

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

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

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

  8. C#LeetCode刷题之#448-找到所有数组中消失的数字(Find All Numbers Disappeared in an Array)

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

  9. LeetCode Find All Numbers Disappeared in an Array

    题意:给出长度为n的数组,数组的数为 1-n,找出没有出现的数 思路:在遍历数组时,将对应的索引的数设置为负数.第二次遍历数组时,索引所在的数为正数,说明是没有出现的 代码如下: public cla ...

最新文章

  1. 超详细支持向量机知识点,面试官会问的都在这里了
  2. 如何解决XML文件中的警告提示“No grammar constraints (DTD or XML Schema) referenced in the document.”...
  3. 查看网关物理地址命令
  4. icp点云匹配迭代最近邻算法
  5. 五 Python之socket网络编程
  6. C++远征之封装篇——常对象成员、常成员函数
  7. vue实现搜索框记录搜索历史_云开发版的微信商城小程序第四章,首页自定义搜索框的实现...
  8. php 函数频率,这是一些使用频率比较高的php函数……
  9. c语言课程建设与改革,C语言程序设计课程教学改革的研究与实践
  10. 基于MATLAB的机械振动合成规律研究,基于MATLAB的机械振动分析研究
  11. iPhone的AFC(Apple File Conduit)
  12. golang使用selenium自动化操作谷歌浏览器
  13. android10隐藏ssid,SSID隐藏手机如何连接 手机连接隐藏ssid无线网络教程
  14. Oracle between and
  15. CentOS调整LVM分区
  16. 82、详解一个交换机能带动多少个网络监控摄像头
  17. Android小图标
  18. android 图片ps处理图片大小,【精】【教程】用Photoshop调整图片大小,以方便上传到论坛...
  19. ZooKeeper源码分析之完整网络通信流程(一)
  20. 中外历史纲要(上)第一单元梳理(部分)

热门文章

  1. 听说过Netflix的Chaos Monkey吗?不用羡慕,我们.NET也有
  2. Hello Blazor:(1)像ASP.NET WebForm一样写代码
  3. 快速打造一个MINI自动发布系统
  4. 二分查找和折半插入排序一块说说-很合适~~~
  5. C# 中 System.Index 结构体和 Hat 运算符(^)的全新用法
  6. ​如何编写高质量的C#代码(一)
  7. 为什么我们需要Logstash,Fluentd等日志摄取器?
  8. 使用ASP.NET Core 3.x 构建 RESTful API - 4.1 面向外部的Model
  9. 程序员自家种水果,新鲜包邮配送
  10. Visual Studio容器项目工程化心得