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 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]
思路:把数组nums中的每一个数所对应的索引的位置的数置为负数。
代码:class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector <int> result;
for(auto n:nums)
nums[abs(n)-1]=-abs(nums[abs(n)-1]);
for(int i=0;i<nums.size();i++)
if(nums[i]>0)
result.push_back(i+1);
return result;
}
};
5. Leetcode 448. Find All Numbers Disappeared in an Array的更多相关文章
- 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 -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 ...
- 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 ...
随机推荐
- R语言重要数据集分析研究——R语言数据集的字段含义
R语言数据集的字段含义 作者:马文敏 选择一种数据结构来储存数据 将数据输入或导入到这个数据结构中 数据集的概念 数据集通常是有数据结构的一个矩形数组,行表示规则,列表示变量. 不同的行业对数据集的行 ...
- Oracle 分页方法研究
1.Oracle 中的三大分页方法 1.1.通过分析函数分页 1.2.通过 ROWNUM 分页 1.3.通过 ROWID 分页 2.Oracle 分页解决方案浅析 2.1.纯后端代码完成分页 2.2. ...
- 【T-SQL进阶】03.执行计划之旅-1
到大牛们说执行计划,总是很惶恐,是对知识的缺乏的惶恐,所以必须得学习执行计划,以减少对这一块知识的惶恐,下面是对执行计划的第一讲-理解执行计划. 本系列[T-SQL]主要是针对T-SQL的总结. T- ...
- Elasticsearch5.0.1安装
最新研究了下ES5.0,ES就是为高可用和可扩展而生的,你可以很方便的增加也减少一个节点.顺便记录下安装过程,也方便以后查看. 1 安装部骤 1.1 安装JDK ES依赖于 ...
- H5个性三级联动日期插件(一)
1. 先看效果:如图 2.如果跟你的需求一样的话,那就抓紧down(当)起来吧! 首先你的页面可能需要很多的开发需求文件: jquery,mobiscroll 等js框架插件等 自己参照官方的demo ...
- 使用dns批量管理普通主机名相关问题
1.dns配置 日常管理主机过程中,会有很多地方需要使用到主机名的,当主机非常多的时候,就不适合使用hosts来管理和同步的所有主机hosts了,这个时候就可以使用dns来管理主机名映射和变动 dns ...
- 【Android Developers Training】 102. 序言:让你的应用获知地点
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Redis 内存管理与事件处理
1 Redis内存管理 Redis内存管理相关文件为zmalloc.c/zmalloc.h,其只是对C中内存管理函数做了简单的封装,屏蔽了底层平台的差异,并增加了内存使用情况统计的功能. void * ...
- 用php+mysql+ajax实现淘宝客服或阿里旺旺聊天功能 之 后台页面
在上一篇随笔中,我们已经看了如何实现前台的对话功能:前台我限定了店主只有一人,店铺只有一个,所有比较单一,但后台就不一样了,而后台更像是我们常见的聊天软件:当然,前台也应该实现这种效果,但原理懂了,可 ...
- selenium 环境搭建
使用selenium + python来搭建环境的步骤: 1. 下载 python 的版本,常用到的有 2.7 和 3.6 2. 下载 selenium 的版本,通过命令进行下载. pip insta ...