博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
阅读量:4641 次
发布时间:2019-06-09

本文共 1643 字,大约阅读时间需要 5 分钟。

[抄题]:

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]

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么去除重复

[一句话思路]:

nums[nums[i] -1] = -nums[nums[i]-1] 每个数字处理一次。没有被处理的正数就是被前面的挤兑了。背吧

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 要做index的数必须取绝对值

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

没有被处理的正数就是被前面的挤兑了.这题[1,n]两端必有的情况太特殊

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

442. Find All Duplicates in an Array 出现两次的:还是考数学啊

 [代码风格] :

class Solution {    public List
findDisappearedNumbers(int[] nums) { //ini List
result = new ArrayList
(); //cc if (nums == null || nums.length == 0) { return result; } //-1 for (int i = 0; i < nums.length; i++) { int val = Math.abs(nums[i]) - 1;//true if (nums[val] > 0) { nums[val] = - nums[val]; } } //check for (int i = 0; i < nums.length; i++) { if (nums[i] > 0) { result.add(i + 1); } } //return return result; }}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/8870388.html

你可能感兴趣的文章
C#判断一个字符串是否是数字或者含有某个数字
查看>>
SVN使用指南
查看>>
【转载】掌 握 3 C ‧ 迎 接 亮 丽 职 涯
查看>>
爬取网站附件
查看>>
java基础图形界面和IO系统
查看>>
javascript学习笔记
查看>>
hdu 3996
查看>>
python第三十九课——面向对象(二)之初始化属性
查看>>
python学习笔记之函数装饰器
查看>>
FEM计算2D瞬态热传导方程
查看>>
四年时光,匆匆而过
查看>>
【php】【psr】psr1 基础编码规范
查看>>
WAF SSI
查看>>
LDAP & it's implementation
查看>>
Apache HttpComponents中的cookie匹配策略
查看>>
冰封的海盗攻略
查看>>
Netty4.x中文教程系列(四) 对象传输
查看>>
linux下find命令使用举例、
查看>>
GET请求在Tomcat中的传递及URI传递
查看>>
ubuntun 服务器与Mac
查看>>