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 ...
随机推荐
- 设置VS2017背景图片
设置方法很简单:安装扩展ClaudiaIDE 1.在这里下载扩展,https://visualstudiogallery.msdn.microsoft.com/9ba50f8d-f30c-4e33-a ...
- 透析SOA、RPC、SOAP、REST、ICE、ESB模型发展史
最初的程序全是单机程序,没有网络,没有RPC,更没有RESTful.程序猿写的东西孤独运行在单机上. 那时的程序猿们语言相通,参与开发同一套系统的团队可以面对面沟通. 网络出现了.网络,也带来变乱.网 ...
- C# 中List<T>与DataSet之间的转换
p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...
- [COCI11-12 #4]删数游戏(贪心+数据结构)
题目描述 题目描述 给出一个N位数字串,删除任意K位,使剩下的数最大.. 输入 第1行:2个整数N和K(1<=K<=N<=500000) 第2行:N个数字(可能为0) 输出 第1行: ...
- Angular4 构建,部署,多环境
- 菜鸟redis初学
该随笔为本人自学redis所遇到的错误,写这些初衷完全是为了避免以后犯相同的错误,如果对别人有帮助,那就相互促进. 在Java中使用redis,首先你的Jdk要能运行,如果没配置好,网上有很多jdk环 ...
- Is ICARSCAN same or old version of LAUNCH X431 Easydiag ?
LAUNCH X431 Easydiag 2.0 is basically the same OBD-II Bluetooth device – but the software supplied w ...
- [ABP] ASP.NET Zero 5.6.0 之 破解日志
继上次ASP.NET Zero 5.5.2的破解https://www.cnblogs.com/VAllen/p/ABP-ASP-NET-Zero-5-5-2-Crack.html之后,现在发布了AS ...
- hdu2054 通过率低是有理由的
这虽然是一道水题,但却巨坑,他题面上不说数据范围,也没有说数据类型,事实就是数据范围巨大,整型实型都有,所以必须用字符串去写,但是又涉及到小数,所以还要删除小数后面多余的0,比如1==1.0000,这 ...
- Ruby学习笔记之升级ruby的版本
升级ruby版本,有时候安装ruby的版本过低,需要进行升级,例如安装在centos6.7安装fpm需要ruby版本在1.9以上. 0x00 主机环境如下 [root@test ~]# cat /et ...