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. ionic 打包安卓包

    一.配置环境: 先按照之前的文章,配置好环境需要: 二.安装 1. 这里前提是 需要安装  node (地址: http://nodejs.cn/download/) 命令: node -v   // ...

  2. JDBC操作数据库之连接数据库

    通过JDBC向数据库中添加数据的时候,使用insert语句实现数据的插入,再SQL语句中的参数可以用占位符"?"来替代,然后通过PreparedStatement对象或者State ...

  3. 监控-CPU使用率

    原始脚本来自TG,自己对部分脚本做了调整,分享出来仅供参考,请勿整篇Copy! 使用以下语句获取[CPU使用率] USE [DBA_Monitor] GO /****** 对象: StoredProc ...

  4. DelayQueue使用示例之KTV包厢记时

    在学习Java 多线程并发开发过程中,了解到DelayQueue类的主要作用:是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走. ...

  5. 动易CMS - 设为首页代码和加入收藏代码(兼容各种浏览器)

    注意: 这里虽然说是兼容,但是有些浏览器的设置就是不支持用js来把页面设为首页,加入收藏夹,只能让用户手动去在浏览器或者按键去设置这些功能,这里说的兼容是指当浏览器有这个设置的时候js会有提示.   ...

  6. mac 安装Beautiful Soup

    Beautiful Soup是一个Python的一个库,主要为一些短周期项目比如屏幕抓取而设计.有三个特性使得它非常强大: 1.Beautiful Soup提供了一些简单的方法和Python术语,用于 ...

  7. Uva 3708 Graveyard

    题意:在周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周上均匀分布. 这就需要移动其中一些原有的雕塑.要求n个雕塑移动的距离最小. (2& ...

  8. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  9. VB.net shell、IO.File.Open、Process.Start、Shellexecute API 运用经验总结

    打开文件还有很多方法,但我了解到运用较多的是上面几种- -,为了防止以后忘记,先把了解到的写下来. 1.Shell 这个看了很多网页,最靠谱的运用方法: Shell("cmd.exe /c ...

  10. Ubuntu 普通用户提升到root权限

    方法一.修改passwd文件 1.编辑passwd文件 sudo vim /etc/passwd 2.找到你想提权的用户(比如test),将用户名后面的数字改成0 找到用户test test:x::: ...