原题链接在这里:https://leetcode.com/problems/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]

题解:

把nums[Math.abs(nums[i])-1]标负. 第二遍iterate 时若nums[i]非负,就表明原array没有i+1, 加到res中.

Time Complexity: O(nums.length). Space: O(1), regardless res.

AC Java:

 public class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
if(nums == null || nums.length == 0){
return res;
} for(int i = 0; i<nums.length; i++){
int index = Math.abs(nums[i])-1;
if(nums[index] > 0){
nums[index] = -nums[index];
}
} for(int i = 0; i<nums.length; i++){
if(nums[i] > 0){
res.add(i+1);
}
} return res;
}
}

也可以把nums[i] swap到对应的index = nums[i]-1位置上.

第二遍iterate时,如果nums[i] != i+1. i+1就是disappeared, 加入res中.

Time Complexity: O(nums.length). Space: O(1), regardless res.

AC Java:

 class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
for(int i = 0; i<nums.length; i++){
if(nums[i]-1>=0 && nums[i]-1<nums.length && nums[i]!=nums[nums[i]-1]){
swap(nums, i, nums[i]-1);
i--;
}
} for(int i = 0; i<nums.length; i++){
if(nums[i] != i+1){
res.add(i+1);
}
} return res;
} private void swap(int [] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

跟上Find All Duplicates in an ArrayFind the Duplicate NumberMissing Number.

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

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

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

  3. leetcode之Find All Numbers Disappeared in an Array

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

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

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

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

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

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

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

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

随机推荐

  1. 【MongoDB】 Windows 安装

    Windows下安装MongoDB,虽然网上有很多攻略,但是还是有很多坑,为了以后少犯错误,特此记录. 1.下载安装包 https://fastdl.mongodb.org/win32/mongodb ...

  2. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  3. HTML 表

    表格: <table></table> 插入一个表格 <tr></tr> 代表一行 其中插入<td></td>单元格       ...

  4. 如何转换WMV到MP3,WMV到MP3播放器

    非常好的软件!!!!!没有注册,可以用.推荐给大家! http://www.daniusoft.com/cn/convert-wmv/wmv-to-mp3.html http://hi.baidu.c ...

  5. 首师大附中互测题:50136142WXY的坑爹百度地图【B006】(可以喝的超大桶水)

    [B006]50136142WXY的坑爹百度地图[难度B]——————————————————————————————————————————————————————————————————————— ...

  6. linux中grep命令详解

    http://jingyan.baidu.com/article/76a7e409e72777fc3b6e158a.html

  7. JS:event对象下的target属性和取消冒泡事件

    1.target 通过获取DOM元素 var box = document.getElementById("box"); document.box.onclick = functi ...

  8. AT指令(中文详解版)(一)

    一 . 一 般 命 令1.AT+CGMI      给出模块厂商的标识.2.AT+CGMM    获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800    或PCS 1900) ...

  9. osgAnimation例子的注释的注释

    osgAnimation例子的注释的注释 转自:http://www.cnblogs.com/sunliming/archive/2011/12/12/2284995.html #include &l ...

  10. <三>JDBC_面向对象思想的体现

    JDBCTools.java import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;i ...