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 = 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.
645. Set Mismatch
The set S
originally contains numbers from 1 to n
. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.
Given an array nums
representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
思路:
448和645具有相似的思路。两道题的共同点在于:元素的大小均为 [1,n]。448要求找出未出现的数字,645要求找出出现了两次的数字和未出现的数字。
由于元素的下标为[0,n-1],则元素的大小减去1得到的即为某个元素的下标,因此可以利用元素大小与下标之间的关系寻找特殊的数字。
对于448,遍历整个数组,通过元素的大小减去1得到下标。若该下标对应的元素为正,则将其乘以-1,变为负数;若该下标对应的元素为负,证明该下标之前已经出现过了一次,不作处理。通过这一次的遍历,仍然为正的元素所对应的下标再加1即为未出现过的元素。
对于645,遍历整个数组,通过元素的大小减去1得到下标。若该下标对应的元素为正,则将其乘以-1,变为负数;若该下标对应的元素为负,证明该下标之前已经出现过了一次,将该下标+1加入result中。通过这一次的遍历,仍然为正的元素所对应的下标再加1即为未出现过的元素。
代码:
448.
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> ans;
for (int i = ; i < (signed) nums.size(); i++) {
int index = abs(nums[i]) - ;
if (nums[index] > )
nums[index] *= -;
else
continue;
}
for (int i = ; i < (signed) nums.size(); i++)
if (nums[i] > )
ans.push_back(i + );
return ans;
}
};
645.
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> ans;
for (int i = ; i < (signed) nums.size(); i++) {
int index = abs(nums[i]) - ;
if (nums[index] > ) {
nums[index] *= -;
} else {
ans.push_back(index + );
}
}
for (int i = ; i < (signed) nums.size(); i++) {
if (nums[i] > )
ans.push_back(i + );
}
return ans;
}
};
448. Find All Numbers Disappeared in an Array&&645. Set Mismatch的更多相关文章
- 【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 ...
- 448. Find All Numbers Disappeared in an Array【easy】
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@python
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- 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 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 解题报告
题目要求 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice a ...
- [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 ...
随机推荐
- c#之AES加密解密
.Net已封装好算法,直接调用即可,代码如下: 转载请注明出处:https://www.cnblogs.com/jietian331/p/9707771.html using System; usin ...
- Fastcgi、CGI 是什么
1.CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. 2.web server(比如说nginx)只是内容的分发者. 比如,如果请求/index ...
- u盘安装centos7.6 最新版本
1,可以按照网上的教程,制作u盘启动 2,然后将u盘插入主机,最重要的是这一步,网上的说法基本上适用这个版本 进入到这个界面: 然后选择Install Centos 7,然后按e键,tab是不管用的 ...
- 阿里云HttpClient跨天之后解析不了域名
也许这是一个少见的情况,我使用HttpClient写了一个调用第三方服务的请求,在本机测试和腾讯云上测试都没有问题,但是放到阿里云之后,刚启动的时候是没有问题的,但是每次过零点之后,就会报异常: ja ...
- pygame 游戏舞台搭建典型应用
#两个文件要放在同一目录中,包括图片 一.搭建游戏舞台主程序 #!/usr/bin/env python3#_*_coding:utf-8_*_ author ==$ VACyp import sys ...
- Delphi调用API函数获取Windows目录信息、获取System目录信息、获取Temp临时文件目录信息
var Str1, Str2: Array[..Max_Path]of Char;//开辟缓冲区 Str3: Array[..]of Char; begin GetWindowsDirectory(@ ...
- 2017.11.19 C语言基础及流水灯实现
/* 从右往左*/ #include <reg52.h> sbit ADDR0 = P1^0; sbit ADDR1 = P1^1; sbit ADDR2 = P1^2; sbit ADD ...
- 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践
2018-2019-2 <网络对抗技术>Exp2 后门原理与实践 1. 后门原理与实践实验说明及预备知识 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0. ...
- Codeforces 1027F Session in BSU - 并查集
题目传送门 传送门I 传送门II 传送门III 题目大意 有$n$门科目有考试,第$i$门科目有两场考试,时间分别在$a_i, b_i\ \ (a_i < b_i)$,要求每门科目至少参加 ...
- 如何使用postman传数组数据
如何使用postman传数组数据 在我们做api接口数据调试的时候,大部分是会用到postman的,一般请求数据的参数都是字符串,但是特殊情况下我们是需要传一个数组数据的,那么为了实现这种需求,究竟该 ...