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]
 public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list = new ArrayList<Integer>(); if (nums == null || nums.length == ) return list; for (int i = ; i < nums.length; i++) {
int val = nums[i];
if (nums[i] != -) {
nums[i] = -;
exchange(nums, val);
}
} for (int i = ; i < nums.length; i++) {
if (nums[i] == -) {
list.add(i + );
}
}
return list;
} public void exchange(int[] nums, int val) {
if (nums[val - ] == val) return; int temp = nums[val - ];
nums[val - ] = val; if (temp != -) {
exchange(nums, temp);
}
}
}
 public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list = new ArrayList<Integer>(); if (nums == null || nums.length == )
return list; for (int i = ; i < nums.length; i++) {
int index = Math.abs(nums[i]) - ;
if (nums[index] > ) {
nums[index] = -nums[index];
}
} for (int i = ; i < nums.length; i++) {
if (nums[i] > ) {
list.add(i + );
}
}
return list;
}
}

Find All Numbers Disappeared in an Array的更多相关文章

  1. leetcode之Find All Numbers Disappeared in an Array

    问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到 ...

  2. 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 = ...

  3. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

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

  8. LeetCode_448. Find All Numbers Disappeared in an Array

    448. Find All Numbers Disappeared in an Array Easy Given an array of integers where 1 ≤ a[i] ≤ n (n  ...

  9. LeetCode Find All Numbers Disappeared in an Array

    原题链接在这里:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 题目: Given an array o ...

  10. [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 ot ...

随机推荐

  1. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 更优美的Oracle数据库上的代码生成器

    代码生成器进行了改进,针对Oracle数据库生成更优美的代码.这样生成出来的代码,更像微软的风格,更像C#.NET的标准规范,阅读起来也更优美.把Oracle表字段名默认大写, 有_分割等进行了优化, ...

  2. 学习C++.Primer.Plus 11 使用类

    1.操作符重载 重载操作符的几个限制: a)         重载的至少有一个操作数是用户定义的类型,这将防止用户为标准类型重载操作符. b)         不能违反操作符原有来的句法规则. c)  ...

  3. Java通过ODBC链接数据库并遍历结果的一个问题

    上一篇文章谈到怎么连接Oracle数据库,其实通过ODBC也差不多,只是driver要换成JdbcOdbcDriver.配置文件如下: driver=sun.jdbc.odbc.JdbcOdbcDri ...

  4. jquery:validate的例子

    该文档转载自 http://ideabean.javaeye.com/blog/363927 官方网站 http://bassistance.de/jquery-plugins/jquery-plug ...

  5. Win7下mysql root账户登录提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)解决方案

    ERROR 1045 (28000): Ac-- password: YES)这个意思是密码不正确,那就修改密码: 如果你是服务器是 windows xp/2000/2003/nt 都可以使用这个方法 ...

  6. 【原创】日志文件清理工具V1.0

    最近公司的系统服务器经常出现磁盘空间不足的情况,登陆服务器发现原来是公司的HR系统日志造成的(插个话题:我们公司的HR系统都实施两年多了还没上线,且不说软件功能如何,服务太TMD差劲了,更可气的是软件 ...

  7. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  8. Win10激活KMS

    Windows 10是目前微软主推的Windows系统,Win10 相对之前的Win7/8是吸取二者之长,发展而来! 目前新装的Windows10默认没有激活的 芝麻开们:http://pan.bai ...

  9. 【JavaScript】js数组操作,由push到那么多

    shift() 定义:删除并返回数组的第一个元素: pop() 定义:删除数组最后一个元素,并返回: push() 定义:在数组后边添加一个或者多个元素,并返回新数组的长度: array.push(& ...

  10. c++的一些陷阱(1)

    class String { public: String(]) { strcpy(p,pp); } ~String() { delete[] p; } char& operator[](in ...