leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接:
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/
题目描述:
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]
题目翻译:
题目要求:给出一个整形数组,长度为n,数组中的整数都在1~n之间,有些数出现一次,有些数出现两次,求出那些没出现的数的集合。 解决方案:
1.
Swap the array, make the a[i] become the (a[i]-1)-th elements in the array.
Traverse the array, if a[i]!=i+1, add i+1 into the answer array.
参考:https://blog.csdn.net/zibingling777/article/details/79008604
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> ans;
int n=nums.size();
for(int i=0;i<n;i++){
int j=i;
while(nums[j]!=nums[nums[j]-1]){
swap(nums[j],nums[nums[j]-1]);
}
}
for(int i=0;i<n;i++){
if(nums[i]!=i+1)
ans.push_back(i+1); //向向量vector,ans后面依次退入元素。
}
return result;
}
};
2 .时间复杂度为O(n),空间复杂度为O(n)的做法。定义一个长度为n的数组temp,全都初始化为0。然后遍历给出的数组,令temp[nums[i]-1]=-1。
最后遍历数组temp,那些值为0的数的位置加上1就是所要求的没有出现的数字。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int l=nums.size();
vector<int> temp(l,);
vector<int> result;
for(int i=;i<l;i++){
temp[nums[i]-]=-;
}
for(int i=;i<l;i++){
if(temp[i]==)
result.push_back(i+);
}
return result;
}
};
3 .时间复杂度为O(n),空间复杂度为O(1)的做法。(符合题目要求的做法,没有使用多余空间)。遍历数组,索引为i,将nums[i]-1所在的位置的数置为负数。再次遍历数组,索引为index,对于每个数组值,如果为负数,就代表index+1这个数已经出现过了,如果为正数,代表没出现过,放到要返回的数组里面。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> result;
int n=nums.size();
for(int i=;i<n;i++){
int m=abs(nums[i])-; //去掉abs()就不能运行,为什么呢
nums[m]=nums[m]>?-nums[m]:nums[m]; //这是什么意思??
}
for(int i=;i<n;i++){
if(nums[i]>)
result.push_back(i+);
}
return result;
}
};
3. 思路
leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)的更多相关文章
- 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 (在数组中找到没有出现的数字)
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"
My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...
- 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 ot ...
- 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找到所有数组中消失的元素
题目 给定一个范围在 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 ...
随机推荐
- SQL语法语句总结
一.SQL语句语法 ALTER TABLE ALTER TABLE 用来更新已存在表的结构. ALTER TABLE tablename (ADD|DROP column datatype [NULL ...
- [poj2002]Squares_hash
Squares poj-2002 题目大意:在笛卡尔坐标系中给出n个点,求这些点可以构成多少个正方形. 注释:$1\le n\le 10^3$,$-2\cdot 10^3\le x , y\le 2\ ...
- jsp pageEncoding属性详解
Java pageEncoding原理详解 首先看下文章解释: 意思是jsp文件本身的编码 巨大的疑问:这里有一个很大的问题,既然你都已经从jsp中读到了这条属性,证明内容读取正确,那你还需要知道这条 ...
- 对Java的初步的认识:
学习JAVA的优势 1) Java广受欢迎 Java是世界上最受欢迎的编程语言之一,有无限多种方法使用Java.据2016年数据显示,Java被评为最受欢迎的编程语言之一2) Java薪资比较好最新 ...
- 【alpha冲刺】随笔合集
Daily Scrum Meeting 第一天 [Alpha]Daily Scrum Meeting第一次 第二天 [Alpha]Daily Scrum Meeting第二次 第三天 [Alpha]D ...
- Java暑期作业
一.假期观影笔记--<熔炉> 影片<熔炉>是根据发生在韩国光州聋哑学校里的真实事件而改编.影片讲述的是在一所聋哑儿童学校里,校长.教务以及老师披着慈善的华丽外衣对学校中的多名未 ...
- from nltk.book import * 出错的解决方法
import nltknltk.download() 在使用上面命令安装了nltk库并运行下载后,再输入from nltk.book import * 往往会出现这样的错误提示: 出现这种错误往往是由 ...
- 结对编程作业——四则运算GUI程序
毛忠庆 201421122088 赵嘉楠 201421122065 源代码存放位置:https://gitee.com/ouwen0819/SiZeYunSuan.git 题目描述 使用 -n 参数控 ...
- Python 远程部署 Fabric
参考文章:http://zmrenwu.com/post/21/ Fabric是一个Python的库,它提供了丰富的同SSH交互的接口,可以用来在本地或远程机器上自动化.流水化地执行Shell命令.因 ...
- DBA 小记 — 分库分表、主从、读写分离
前言 我在上篇博客 "Spring Boot 的实践与思考" 中比对不同规范的 ORM 框架应用场景的时候提到过主从与读写分离,本篇随笔将针对此和分库分表进行更深入地探讨. 1. ...