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 = ...
随机推荐
- 全栈项目|小书架|服务器开发-Koa2 参数校验处理
为什么需要做参数校验 在开发中,无论是App开发还是服务器接口开发, 我们无法去预测用户传入的数据,因此参数(数据)校验是开发中不可或缺的一环. 例如像App的注册登录表单提交页面,就要做好多层的判断 ...
- idea 终端terminal修改git bash
1 Ctrl+Alt+s 打开设置修改shell path 2 Alt +12 或View + Tool Windows + Terminal 修改成功
- centos 7 安装nginx并启动(笔记)
参考 https://www.cnblogs.com/liujuncm5/p/6713784.html Nginx 是 C语言 开发 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译 ...
- ② Python3.0 运算符
Python3.0 语言支持的运算符有: 算术运算符.比较(关系)运算符.赋值运算符.逻辑运算符.位运算符.成员运算符.身份运算符.运算符优先级 一.算术运算符 常见的算术运算符有+,-,*,/,%, ...
- 【洛谷 P4137】 Rmq Problem / mex(主席树)
题目链接 容易发现,可能答案只有\(0\).每个数,每个数\(+1\) 于是把这\(2n+1\)个数建立一个权值线段树,可持久化一下,每个节点记录这个子树中最后加入数加入的时间的最小值\(latest ...
- Lua for - ipairs,pairs,# 遍历的运行时间
- 【转载】Javascript使用Math.random()随机数函数生成1至1000的随机数
在Javascript代码编写过程中,有时候我们需要使用Js来生成随机数,清除ajax的get请求缓存的时候我们会带上一个随机数来解决此问题,此外在其他应用中也可能使用到随机数,在Javascript ...
- 设置redis开机自动启动
注意:win7执行命令前面可不需要加路径,win10必须要加路径 命令: redis-server --service-install redis.windows.conf 执行完成之后,打开服务管理 ...
- vue-cli脚手架——3.0版本项目案例
一.[准备工作] node与git部分见vue-cli2.0搭建案例 vue-cli3.0是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cli 全局安装的 npm ...
- array_reduce() 与 array_map()
相似部分: 二者同为 处理数组函数,可遍历 数组中的每一个元素, 对其通过 function callback(){} 处理. 不同处: 参数: array_reduce( array, callba ...