题目描述

给定n个数字的数组,里面的值都是1-n,但是有的出现了两遍,因此有的没有出现,求没有出现值这个数组中的值有哪些。

要求不能用额外的空间(除了返回列表之外),时间复杂度n

思路

因为不能用额外空间并且时间是O(n),所以不能用排序或者hash

通过在对应位置的值去确定下一个位置,一直到遍历完。其实自己也没有很成熟的思想。

比如对于[4,3,2,7,8,2,3,1]:

A[0],4=>找到a[3],7=>a[6],3=>a[2]====注意有可能出现循环,行不通。

本来以为行不通,后来经过思考,可以行得通,只不过复杂度是O(k*n),k是重复的数字的个数,也是缺少的数字的个数。思路是这样的A[0],4,并且把A[0]标记为-1=>a[3],7并把4放到a[3]=>a[6],3,把7放到a[6],=>a[2],2,把3放到a[2]=>a[1],3,把2放到a[3],===>想把3放到a[2],只是a[2]已经=3,所以3是数组中重复的。一个循环结束,从A[0]又开始,找到!=-1,并且!=i+1的a[i],把里面的值放到对应的位置上。一直到所有位置不是-1就是i+1结束。位置是-1的,对应空缺数值i+1。

代码

    public static List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> result=new ArrayList<>();
boolean isEnding=false;//用isEnding检查是否所有位置不是-1就是i+1
boolean isExist=false;//是否从上一个位置取出了数字
int i=0;//代表找到的位置
while(!isEnding){
isEnding=true;
i=0;
while(i<nums.length){
if((nums[i]!=i+1)&&(nums[i]!=-1)){//没有重复,可以放进去
isEnding=false;
int tmp=nums[i];
if(isExist){//如果有可以放进去的值,就先取出来再放进去
nums[i]=i+1;
}else{//否则,只能取出来,并把-1放进去
nums[i]=-1;
}
i=tmp-1;
isExist=true;
}else{//如果相等,那么就重复了不需要放进去,直接往前面去;碰到了-1,如果需要放进去,就先放进去,再往后面去
if(isExist&&nums[i]==-1){
nums[i]=i+1;
}
i++;
isExist=false;
}
}
}
for(i=0;i<nums.length;i++){
if(nums[i]==-1){
result.add(i+1);
}
}
return result;
}

分析

看了看solution,是把所有的对应值的对应位置上的值都置为相反数:nums[|nums[i]| -1] = -|nums[|nums[i]|-1]|,如果第二次遍历发现它>0,那么它是没见过的。比如[4,3,2,7,8,2,3,1],遍历一遍之后,得到[-4,-3,-2,-7,8,2,-3,-1],所以是8、2对应位置的数值没有在数组出现过,也就是5,6

[LeetCode] 448. Find All Numbers Disappeared in an Array 找到数组中消失的数字的更多相关文章

  1. LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素

    题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...

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

  3. leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

  4. 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字

    给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...

  5. LeetCode "448. Find All Numbers Disappeared in an Array"

    My first reaction is to have an unlimited length of bit-array, to mark existence. But if no extra me ...

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

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

  8. LeetCode: 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 an ...

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

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

随机推荐

  1. [源码解析] PyTorch分布式优化器(1)----基石篇

    [源码解析] PyTorch分布式优化器(1)----基石篇 目录 [源码解析] PyTorch分布式优化器(1)----基石篇 0x00 摘要 0x01 从问题出发 1.1 示例 1.2 问题点 0 ...

  2. adjust, administer

    adjust to just, exact. In measurement technology and metrology [度量衡学], calibration [校准] is the compa ...

  3. Git提交规范

    Commit message 的格式 每次提交,Commit message 都包括三个部分:Header,Body 和 Footer. <type>(<scope>): &l ...

  4. Android: EditText设置属性和设置输入规则

    1.EditText输入限制规则 在xml:EditText 设置属性 android:digits="ABCDE123&*" ABCDE123&*是你的限制规则 ...

  5. 【Python】【Basic】【数据类型】运算符与深浅拷贝

    运算符   1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 三元运算 三元运算(三目运算),是对简单的条件语句的缩写. # 书写格式 result = 值1 if 条件 ...

  6. 编译安装redis之快速增加redis节点

    #: 下载安装包 [root@localhost ~]# wget http://download.redis.io/releases/redis-4.0.14.tar.gz #:解压 [root@l ...

  7. vue2.x入门学习

    vue安装 # 最新稳定版本 $ npm install vue # 最新稳定 CSP 兼容版本 $ npm install vue@csp 引包 cd /d/vue/demo cnpm instal ...

  8. redis入门到精通系列(六):redis的事务详解

    (一)事务的概念 谈到数据库的高级应用,不可避免会谈到事务.熟悉mysql的朋友们对事务肯定不陌生,简单来讲事务就是控制一个数据库操作序列要么全部执行要么全部不执行.今天我们就来了解redis中的事务 ...

  9. awk统计命令(求和、求平均、求最大值、求最小值)

    本节内容:awk统计命令 1.求和 cat data|awk '{sum+=$1} END {print "Sum = ", sum}' 2.求平均 cat data|awk '{ ...

  10. VueAPI 2 (生命周期钩子函数)

    所有的生命周期钩子自动绑定 this 上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法. beforeCreate 在实例初始化之后,此时还不 ...