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 = ...
随机推荐
- Mysql字符集之utf8和utf8mb4的使用问题
Mysql之utf8和utf8mb4的区别 最近在项目中使用Mysql数据库保存emoji表情
- docker 入坑2
上一节我们安装好了docker,那么这节我们讲一下docker基本命令使用 查看版本 $ sudo docker --version 返回:Docker version 18.09.0, build ...
- fiddler数据过滤功能
设置会话过滤的菜单如下图: 1.勾选Use Filters选项表示使用过滤设置,不勾选则不使用 2.Actions:有四个选项 Run Filterset now:立即运行过滤设置: Load Fil ...
- ③ Python3.0 数字类型
Python3 的六个标准数据类型中:不可变数据(3 个):Number(数字).String(字符串).Tuple(元组):可变数据(3 个):List(列表).Dictionary(字典).Set ...
- 阿里云ssl协议发布qq邮件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- MySQL5.7.16安装及配置
一.下载 下载页面http://dev.mysql.com/downloads/mysql/ 选择系统平台后,点击download(根据系统选择64或32位) 二.配置 1.下载成功后,解压安装包到要 ...
- Java 之 Servlet 3.0
Servlet 3.0 好处: 支持注解配置,不需要 web.xml 文件了. 步骤: (1)创建 Java EE 项目,注意:JavaEE 版本必须6.0以上才支持Servlet3.0,可以不创建 ...
- Android gradle用exclude排除引用包中的dependency引用
项目突然编译不通过,报如下错误 FAILURE: Build failed with an exception. * What went wrong: Execution fai ...
- iOS加密之AES
心急的童鞋直接看这里Demo 运行之后可以去在线加密网站验证 AES(Advanced Encryption Standard)高级加密标准,又称Rijndael加密法,是美国联邦政府采用的一种区块加 ...
- Http状态码502问题复盘
问题原因分析:502 bade gateway 一般都是upstream出错,对于PHP,造成502的原因常见的就是脚本执行超过timeout设置时间,或者timeout设置过大,导致PHP进程长时间 ...