leetcode解题报告(33): 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]
分析
这道题要是先排序,再计算,就会十分简单,然而平均性能最好的快排也只有O(nlgn),明显不符合题目要求。
一开始想了挺久都没解出来,后来看了讨论区 https://discuss.leetcode.com/topic/65944/c-solution-o-1-space
的答案才知道的。
这种解法还是挺巧妙的,我怎么就想不到呢。。。还是做的题目太少了啊。。
解法如下:
由于数组大小是n,且元素值范围为[1,n],因此可以从下标和元素值大小的联系入手。
遍历该数组,对于每一个元素,都将对应的元素置为负数,如对于题目所给数组,第一个元素为4,则将第四个元素7(对应的数组的下标为3)置为负数,如果是负数,则不作处理。遍历结束后,数组元素 变为:
[-4,-3,-2,-7,8,2,-3,-1]
第5个元素8需要下标为4的元素来将其置为负数,由于数组中没有5这个元素,因此该元素值仍为正数8,同理,第6个元素(对应的下标为5)也是正数。
因此,只需要再遍历一次数组,每遇到一个正数,就说明对应的元素不在数组中,将该元素压入vector中,遍历结束后,即可得到结果。
代码如下:
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
if(nums.size() == 0)return nums;
vector<int>ret;
for(int i = 0; i != nums.size(); ++i){
int m = abs(nums[i]) - 1;
nums[m] = nums[m] > 0 ? -nums[m] : nums[m];
}
for(int i = 0; i != nums.size(); ++i){
if(nums[i] > 0)ret.push_back(i + 1);
}
return ret;
}
};
leetcode解题报告(33): Find All Numbers Disappeared in an Array的更多相关文章
- [LeetCode&Python] Problem 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解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- leetcode之Find All Numbers Disappeared in an Array
问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到 ...
- 【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 ...
- LeetCode_448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array Easy Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 448. Find All Numbers Disappeared in an Array&&645. Set Mismatch
题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...
随机推荐
- [LOJ2002] [SDOI2017] 序列计数
题目链接 LOJ:https://loj.ac/problem/2002 洛谷:https://www.luogu.org/problemnew/show/P3702 Solution 考虑补集转换, ...
- go 学习笔记(4) package
package name 尽量与目录名称一样 package name: 表示代码文件所属的包
- Thread 与 ThreadLocal
@Testpublic void testThread() { Thread thread = Thread.currentThread(); System.out.println("thr ...
- pytorch 0.4.0迁移指南
总说 由于pytorch 0.4版本更新实在太大了, 以前版本的代码必须有一定程度的更新. 主要的更新在于 Variable和Tensor的合并., 当然还有Windows的支持, 其他一些就是支持s ...
- git重置账号密码
1.打开控制面板(快捷打开win+R,输入control) 2.点击打开用户账户 3.点击凭据管理器 4.点击windows凭据删除你的git凭据即可
- 如何在 WPF 中获取所有已经显式赋过值的依赖项属性
原文:如何在 WPF 中获取所有已经显式赋过值的依赖项属性 获取 WPF 的依赖项属性的值时,会依照优先级去各个级别获取.这样,无论你什么时候去获取依赖项属性,都至少是有一个有效值的.有什么方法可以获 ...
- bootstrap-datetimepicker 日期控件起始时间和结束时间
项目中经常会用到起止时间,如下图: 需要引用以下几个文件: <link href="~/lib/bootstrap/dist/css/bootstrap.min.css" r ...
- YII 的SPA 写法
'use strict'; var findToolbar = function () { return document.querySelector('#yii-debug-toolbar'); } ...
- Java调用WebService方法总结(7)--CXF调用WebService
CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...
- python day 12: 选课系统
目录 python day 12 1. 通过类来创建选课系统 1.1 类库models.py 2. 配置文件setting.py 3. administrator.py 4. student.py p ...