博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode-448-Find All Numbers Disappeared in an Array]
阅读量:6848 次
发布时间:2019-06-26

本文共 1084 字,大约阅读时间需要 3 分钟。

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]

思路:

The idea is very similar to problem 442. Find All Duplicates in an Array: .

First iteration to negate values at position whose equal to values appear in array. Second iteration to collect all position whose value is positive,

which are the missing values. Complexity is O(n) Time and O(1) space.

vector
findDisappearedNumbers(vector
& nums) { int len = nums.size(); for (int i = 0; i
0 ? -nums[m] : nums[m]; } vector
res; for (int i = 0; i
0) res.push_back(i + 1); } return res; }

参考:

转载于:https://www.cnblogs.com/hellowooorld/p/6846043.html

你可能感兴趣的文章
Leetcode 20. Valid Parentheses
查看>>
VM 监控信息布局
查看>>
nat转发
查看>>
域后续之golden ticket
查看>>
NSOutlineView使用笔记(二)
查看>>
centos7下如何安装mysql 亲测
查看>>
SSH防止暴力破解 shell script
查看>>
真是讨人厌
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
如何编辑UG中打开文件的历史记录信息(history.pax)?
查看>>
LeetCode 5回文数
查看>>
mysql的安装与配置
查看>>
Linux运维系统工程师系列---23
查看>>
S3C2410串口驱动程序
查看>>
源码安装http的-2.4.4
查看>>
我的友情链接
查看>>
java内存分配和String类型的深度解析
查看>>
One_install postifx is shell
查看>>