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]

题目标签:Array
  题目给了我们一个nums array,其中的数字 范围是从1到n,让我们找到没有出现过的数字。
  这道题目和 #442 基本思想一样,因为数字范围是 1到n,index 范围是 0 到n-1,建立一个数字 和 index 的1对1 映射。
  遍历数组,对于每一个num,去 index = num - 1 的位置,把映射的数字变为负数,标记num已经出现过。
  最后再次遍历数组,把正数的数字的index + 1 加入 List,因为所有负数的数字,代表了有出现过的数字去标记过。只有正数的数字,没有数字去标记。
 

Java Solution:

Runtime beats 81.65%

完成日期:05/09/2017

关键词:Array

关键点:把num 和 nums[num - 1] 做1对1的映射

 public class Solution
{
public List<Integer> findDisappearedNumbers(int[] nums)
{
List<Integer> dis = new ArrayList<>(); // iterate nums array.
for(int i=0; i<nums.length; i++)
{
int index = Math.abs(nums[i]) - 1; // the nums[i] may be negative.
if(nums[index] > 0) // mark corresponding number to negative if it is positive.
nums[index] *= -1; }
// iterate nums again to add i if nums[i] is positive.
for(int i=0; i<nums.length; i++)
{
if(nums[i] > 0)
dis.add(i+1);
} return dis;
}
}

参考资料:

www.cnblogs.com/grandyang/p/6222149.html

LeetCode 题目列表 - LeetCode Questions List

LeetCode 448. Find All Numbers Disappeared in an Array (在数组中找到没有出现的数字)的更多相关文章

  1. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  2. LeetCode "448. Find All Numbers Disappeared in an Array"

    My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...

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

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

  5. LeetCode: 448 Find All Numbers Disappeared in an Array(easy)

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

  6. LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

  7. [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字

    题目描述 给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些. 要求不能用额外的空间(除了返回列表之外),时间复杂度n 思路 因为不能用额外 ...

  8. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

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

随机推荐

  1. Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran

    今天在使用一对多,多对一保存数据的时候出现了这个错误 Hibernate错误: Exception in thread "main" org.hibernate.Transient ...

  2. Identifying Duplicate Indexes

    本文是在阅读<Troubleshooting SQL Server>->Chapter 5: Missing Indexes->Identifying Duplicate In ...

  3. Java学习笔记三---unable to launch

    环境配置好后,在eclipse下编写HelloWorld程序: ①创建新工程 ②创建.java文件,命名为HelloWorld ③在源文件中添加main方法,代码如下: public void mai ...

  4. idea启动tomcat报错:Error during artifact deployment. See server log for details.

    出现这种情况的原因老夫猜想是改变了artifact然而tomcat的配置中的artifact没有重新配就会出现这种报错 打开tomcat配置 删除原来的artifact 新添加artifact 保存 ...

  5. 使用apache反向代理tomacat

    起源 在大部分的生产环境中,基本上使用的都是java程序,从而促进了各种应用程序中间件的产生,在这里大概有几种,tomcat作为最著名的开源servlet容器,jboss也是开源的,而且有管理界面,主 ...

  6. [err] 1055

    本人mysql安装在ubuntu16.04上,mysql版本是5.7.19:在创建表和插入数据时报了 [Err] 1055 - Expression #1 of ORDER BY clause is ...

  7. 深入理解计算机系统chapter7

    链接:将各种代码和数据部分收集起来并组合成为单一文件的过程,这个文件可被加载到存储器并执行. 在运行时,和一个在存储器中的程序链接起来 二.静态链接库与动态链接库 静态连接库就是把(lib)文件中用到 ...

  8. Conscription poj3723(最大生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6870   Accepted: 2361 Desc ...

  9. JavaWeb(一)Servlet中乱码解决与转发和重定向的区别

    前言 前面其实已经把Servlet中所有的内容都介绍完了,这篇讲补充一点乱码和重定向与转发之间的区别! 一.request请求参数出现乱码问题 1.1.get请求 1)乱码示例 get请求的参数是在u ...

  10. 和团队齐头并进——敏捷软件开发的Scrum的学习

    敏捷开发的介绍 概念 更强调程序员团队与业务专家之间的紧密协作.面对面的沟通(认为比书面的文档更有效).频繁交付新的软件版本.紧凑而自我组织型的团队.能够很好地适应需求变化的代码编写和团队组织方法,也 ...