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 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]

解法一:

 class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int len = nums.size();
for(int i=; i<len; i++) {
int m = abs(nums[i])-; // index start from 0
nums[m] = nums[m]> ? -nums[m] : nums[m];
}
vector<int> res;
for(int i = ; i<len; i++) {
if(nums[i] > ) res.push_back(i+);
}
return res;
}
};

参考了大神的解法和解释:First iteration to negate values at position whose equal to values appear in array. Second iteration to collect all position whose value is positive, which are the missing values. Complexity is O(n) Time and O(1) space.The basic idea here is to label all appeared numbers in the array. Since we don't want to introduce extra space and given all numbers are positive(from 1 to n), negate the value in the corresponding position is one choice. Ex, for input like [1, 2, 2], after the first sweep, it becomes [-1, -2, 2]. At position index=2, there is a positive value, which means it corresponding value 3 is not showing.
Hope this simple example gives you some lead :-)

假如我们给定的序列为[8,7,1,2,8,3,4,2],下面给出推导:

每行标记颜色的表示该次改变的值,黄色为第一次搞,即需要变为负值;绿色为非第一次搞,已经为负值了,就不要改变了。最后可以看到剩下为正数的下标就是我们的所求。

再解释一下为什么必须要abs(nums[i]),是因为题目中数组的长度就是n,而且最大值也是n为止,那么既少元素又要凑够n个数,里面必然是有重复的元素的,那么对于重复的元素我我们一开始置为了负值,如果不取abs的话,下次再来就会造成数组越界的。比如下面就是越界的例子:

解法二:

 class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
int n = nums.length;
for (int i = ; i < nums.length; i ++)
{
nums[(nums[i]-) % n] += n;
} for (int i = ; i < nums.length; i ++)
{
if (nums[i] <= n) res.add(i+);
} return res;
}
}

另外一位大神的解法:We can change back the value after find out result;How to do it?We can simply traverse array again and for each element, call nums[i] = nums[i] % n; to restore.

最新文章

  1. spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )
  2. CF2.BC
  3. size_t 类型
  4. C# 不重复的随机数
  5. /etc/xinetd.conf 和 /etc/xinetd.d/*【新网络服务配置】
  6. [Noi2016十连测第五场]二进制的世界
  7. 一致性 hash 算法
  8. HDU 3966:Aragorn&#39;s Story(树链剖分)
  9. html onclick 传参数
  10. (转载)PHP常用函数
  11. 《JavaScript 闯关记》之 BOM
  12. Windows下载安装jmeter
  13. Windows Latex 中日文字体设置例
  14. 《java入门》第一季之类(String类字符串一旦被赋值就没法改变)
  15. SpringBoot与日志框架2(日志内斗)
  16. 软件光栅器实现(二、VS和PS的运作,法线贴图,切空间的计算)
  17. Linux Mint Mate 常用
  18. 通过监控Nginx日志来实时屏蔽高频恶意访问的IP
  19. HDU 4122 Alice&#39;s mooncake shop (RMQ)
  20. VS2015 Offline Help Content is now available in 10 more languages!

热门文章

  1. 【母函数】hdu1028 Ignatius and the Princess III
  2. 1.6(学习笔记)Session
  3. RxJava 1.x 理解-1
  4. 解密所有APP运行过程中的内部逻辑(转)
  5. React Conf 2017 干货总结 1: React + ES next = ♥
  6. verynginx +nginx_upstream_check_module模块,负载均衡检查模块。
  7. UNDO表空间损坏导致数据库无法OPEN
  8. PostgreSQL数据库的安装与配置
  9. 轻松学习JavaScript十二:JavaScript基于面向对象之创建对象(二)
  10. 使用Visual Studio的动态连接库创建通用数据库连接对话框