原题链接在这里: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. intellij idea 插件 ideaVim 用法

    intellij idea 插件 ideaVim - Genji_ - 博客园http://www.cnblogs.com/nova-/p/3535636.html IdeaVim插件使用技巧 - - ...

  2. 1.oracle 12c基础

    1.查看数据库的创建模式 SQL> select name,cdb,con_id from v$database; NAME      CDB     CON_ID ---------    - ...

  3. CozyRSS开发记录1-原型图与Grid

    CozyRSS开发记录1-原型图与Grid 1.使用MockPlus画出最简陋的原型图 这个界面参考了目前我最常使用的RSS阅读-傲游浏览器的内置RSS阅读器.主体框架划分为上.左下.右下三块,分别是 ...

  4. java基础(四)

    一.面向对象的三个基本特征: 1.封装,将对象的实现细节隐藏起来,并通过公共接口暴露相关功能: 2.继承,代码复用的表现,当子类继承父类后,子类作为一种特殊的父类,直接获得父类的属性和方法: 3.多态 ...

  5. BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6014  Solved: 2503[Submit ...

  6. 判断IP地址是否合法类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IPFl ...

  7. compass General 常用api学习[Sass和compass学习笔记]

    compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...

  8. MySQL查询语句(select)详解(1)

    1.查询记录 select*from 表名 [where 条件];eg:select*from students;//查询 students 表中所有记录,所有字段的值都显示出来select fiel ...

  9. 不安装Oracle客户端使用PLSQL连接Oracle数据库的方法

    1,下载PL\SQL http://dl8.cr173.com/soft1/PLSQLDeveloper10_ha.zip(这个是我下载的,带破解和汉化); 2,下载完后傻瓜式安装 ,这里说下,1是P ...

  10. BSBuDeJie_01

    一. 基本配置 1 项目图标 将图片直接拖入Assets-AppIcon 2 启动图片     3 软件名称   4 删除Main.stroryboard   5 设置窗口的根控制器 - (BOOL) ...