[LeetCode] 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]
这道题让我们找出数组中所有消失的数,跟之前那道Find All Duplicates in an Array极其类似,那道题让找出所有重复的数字,这道题让找不存在的数,这类问题的一个重要条件就是1 ≤ a[i] ≤ n (n = size of array),不然很难在O(1)空间和O(n)时间内完成。三种解法也跟之前题目的解法极其类似。首先来看第一种解法,这种解法的思路路是,对于每个数字nums[i],如果其对应的nums[nums[i] - 1]是正数,我们就赋值为其相反数,如果已经是负数了,就不变了,那么最后我们只要把留下的整数对应的位置加入结果res中即可,参见代码如下:
解法一:
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
int idx = abs(nums[i]) - ;
nums[idx] = (nums[idx] > ) ? -nums[idx] : nums[idx];
}
for (int i = ; i < nums.size(); ++i) {
if (nums[i] > ) {
res.push_back(i + );
}
}
return res;
}
};
第二种方法是将nums[i]置换到其对应的位置nums[nums[i]-1]上去,比如对于没有缺失项的正确的顺序应该是[1, 2, 3, 4, 5, 6, 7, 8],而我们现在却是[4,3,2,7,8,2,3,1],我们需要把数字移动到正确的位置上去,比如第一个4就应该和7先交换个位置,以此类推,最后得到的顺序应该是[1, 2, 3, 4, 3, 2, 7, 8],我们最后在对应位置检验,如果nums[i]和i+1不等,那么我们将i+1存入结果res中即可,参见代码如下:
解法二:
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != nums[nums[i] - ]) {
swap(nums[i], nums[nums[i] - ]);
--i;
}
}
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != i + ) {
res.push_back(i + );
}
}
return res;
}
};
下面这种方法是在nums[nums[i]-1]位置累加数组长度n,注意nums[i]-1有可能越界,所以我们需要对n取余,最后要找出缺失的数只需要看nums[i]的值是否小于等于n即可,最后遍历完nums[i]数组为[12, 19, 18, 15, 8, 2, 11, 9],我们发现有两个数字8和2小于等于n,那么就可以通过i+1来得到正确的结果5和6了,参见代码如下:
解法三:
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
vector<int> res;
int n = nums.size();
for (int i = ; i < n; ++i) {
nums[(nums[i] - ) % n] += n;
}
for (int i = ; i < n; ++i) {
if (nums[i] <= n) {
res.push_back(i + );
}
}
return res;
}
};
类似题目:
Find All Duplicates in an Array
参考资料:
https://discuss.leetcode.com/topic/65944/c-solution-o-1-space
https://discuss.leetcode.com/topic/66063/5-line-java-easy-understanding
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字的更多相关文章
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- 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 ...
- 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...
- LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- Leetcode448.Find All Numbers Disappeared in an Array找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能在不 ...
- 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素
[抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...
- 215. Kth Largest Element in an Array找出数组中第k大的值
堆排序做的,没有全部排序,找到第k个就结束 public int findKthLargest(int[] nums, int k) { int num = 0; if (nums.length &l ...
- LeetCode——Find All Numbers Disappeared in an Array
LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...
- 11.Find All Numbers Disappeared in an Array(找出数组中缺失的数)
Level: Easy 题目描述: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements ...
随机推荐
- CSS知识总结(六)
CSS常用样式 4.段落样式 1)行高 控制段落内每行高度 line-height : normal | length 例子 源代码: /* CSS代码 */ .normal{ line-height ...
- UWP简单示例(一):快速合成音乐MV
准备 IDE:Visual Studio 2015 为你的项目安装Nuget包 SharpDx.XAudio2 为你的项目安装Nuget包 Win2D.UWP 了解并学习:Win2D官方博客 了解并学 ...
- 用Middleware给ASP.NET Core Web API添加自己的授权验证
Web API,是一个能让前后端分离.解放前后端生产力的好东西.不过大部分公司应该都没能做到完全的前后端分离.API的实现方式有很 多,可以用ASP.NET Core.也可以用ASP.NET Web ...
- 来份ASP.NET Core尝尝
0x01.前言 学习ASP.NET Core也有一段时间了,虽说很多内容知识点还是处于一知半解的状态,但是基本的,还是 略懂一二.如果有错误,还望见谅. 本文还是和之前一样,Demo+在Linux下运 ...
- Entity Framework 教程——安装Entity Framework环境
安装Entity Framework环境 Entity Framework 5.0 API分布在两个地方,一个可在NuGet包管理器中找到,一个存在于.NET framework中..NET fram ...
- 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(六)
(六)在Website Cloud中添加site 1新建Website,并打开 使用前面创建的用户 newbee@waplab.com 登录租户Portal,新建一个website 新建完成后, 可以 ...
- php在没有登录的情况下自动跳转到登录页
<?php namespace Home\Controller; use Think\Controller; class BaseController extends Controller{ / ...
- 【视频处理】YUV与RGB格式转换
YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...
- Consul的一个更新:服务端节点故障后重连
研究了一段时间Consul,想写个攻略来着,但太赖了而且表达能力非正常人...今天发现HashiCorp果然接纳大众意见改了点东西.. 场景是: 假如Consul集群内有三个Server Node 时 ...
- echarts学习总结
ECharts学习总结使用步骤1 引入ECharts <script src="echarts.min.js"></script>2 绘制一个简单的图表 为 ...