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 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]
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 (在数组中找到没有出现的数字)的更多相关文章
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- [LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字
题目描述 给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些. 要求不能用额外的空间(除了返回列表之外),时间复杂度n 思路 因为不能用额外 ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- 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 ...
随机推荐
- ionic 打包安卓包
一.配置环境: 先按照之前的文章,配置好环境需要: 二.安装 1. 这里前提是 需要安装 node (地址: http://nodejs.cn/download/) 命令: node -v // ...
- JDBC操作数据库之连接数据库
通过JDBC向数据库中添加数据的时候,使用insert语句实现数据的插入,再SQL语句中的参数可以用占位符"?"来替代,然后通过PreparedStatement对象或者State ...
- 监控-CPU使用率
原始脚本来自TG,自己对部分脚本做了调整,分享出来仅供参考,请勿整篇Copy! 使用以下语句获取[CPU使用率] USE [DBA_Monitor] GO /****** 对象: StoredProc ...
- DelayQueue使用示例之KTV包厢记时
在学习Java 多线程并发开发过程中,了解到DelayQueue类的主要作用:是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走. ...
- 动易CMS - 设为首页代码和加入收藏代码(兼容各种浏览器)
注意: 这里虽然说是兼容,但是有些浏览器的设置就是不支持用js来把页面设为首页,加入收藏夹,只能让用户手动去在浏览器或者按键去设置这些功能,这里说的兼容是指当浏览器有这个设置的时候js会有提示. ...
- mac 安装Beautiful Soup
Beautiful Soup是一个Python的一个库,主要为一些短周期项目比如屏幕抓取而设计.有三个特性使得它非常强大: 1.Beautiful Soup提供了一些简单的方法和Python术语,用于 ...
- Uva 3708 Graveyard
题意:在周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周上均匀分布. 这就需要移动其中一些原有的雕塑.要求n个雕塑移动的距离最小. (2& ...
- Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- VB.net shell、IO.File.Open、Process.Start、Shellexecute API 运用经验总结
打开文件还有很多方法,但我了解到运用较多的是上面几种- -,为了防止以后忘记,先把了解到的写下来. 1.Shell 这个看了很多网页,最靠谱的运用方法: Shell("cmd.exe /c ...
- Ubuntu 普通用户提升到root权限
方法一.修改passwd文件 1.编辑passwd文件 sudo vim /etc/passwd 2.找到你想提权的用户(比如test),将用户名后面的数字改成0 找到用户test test:x::: ...