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 ...
随机推荐
- ecshop开发帮助
http://www.ecshop.com/template_tutorial/ ECSHOP模板结构说明 http://help.ecshop.com/index.php ECSHOP帮助中心 ht ...
- 当mysql遇上PHP
博客提纲 利用PHP连接mySQL数据库 两套接口:面向对象和面向过程 实现写改删查(CUBD)实例 通过prepare语句处理相同类型的不同SQL语句 通过bind_param()绑定参数,及相关注 ...
- ES6的开发环境搭建
在搭建es6开发环境之前,先简单介绍一下es6. ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在2015年6月正式发布了.它的目标,是使得 Java ...
- response.sendRedirect页面跳转的方法总结——实习第六天
今天想到要写这个博客完全是因为今天在这上面摔了一跤,同时也意识到了对于一个学Java的人来讲,知其然并且知其所以然是有多么的重要. 今天报了一个错误,讲的是空指针异常,原因就是在response.se ...
- Lamp单独安装(windows下)
安装的软件清单:apache_2.2.9-win32-x86-openssl-0.9.8h-r2.msimysql-5.1.28-rc-win32.zipphp-5.2.6-Win32.zipphpM ...
- php隔行换色输出表格
<?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...
- thinkphp中fetch渲染模板的处理
<script type="text/javascript"> function xiugai(elm){ var formData1=$("#a1_&quo ...
- Selenium chrome配置不加载图片Python版
环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit) f ...
- python3中socket套接字的编码问题解决
一.TCP 1.tcp服务器创建 #创建服务器 from socket import * from time import ctime #导入ctime HOST = '' #任意主机 PORT = ...
- meta小结
mate 标签定义及使用说明 元数据(Metadata)是数据的数据信息. 标签提供了 HTML 文档的元数据.元数据不会显示在客户端,当时会被浏览器解析. META元素通常用于指定网页的描述,关键词 ...