[抄题]:

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<Integer> findDisappearedNumbers(int[] nums) {
//ini
List<Integer> result = new ArrayList<Integer>(); //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;
}
}

448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. test20181219(期末考试)

    Written with StackEdit. \(noip\)爆炸后就好久没考试了...结果今天又被抓去,感觉很慌啊... 考完了.过来填坑. T1 Description 使得\(x^x\)达到或 ...

  2. LeetCode Kill Process

    原题链接在这里:https://leetcode.com/problems/kill-process/description/ 题目: Given n processes, each process ...

  3. IDEA Generate pojo(data first)基于 spring data jpa - code

    基于 idea 的 pojo生成 右侧菜单栏 Database->New( + 图标)->Data source-> mysql(根据自己的数据源选择) 使用: 代码如下: POJO ...

  4. 51nod 1362 搬箱子——[ 推式子+组合数计算方法 ] [ 拉格朗日插值 ]

    题目:http://www.51nod.com/Challenge/Problem.html#!#problemId=1362 方法一: 设 a 是向下走的步数. b 是向右下走的步数. c 是向下走 ...

  5. 最长的回文串——hdu3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 abcba 5 aab 2 在一个字符串里寻找一条最长的回文串 比较直接的想法是枚举中心点 然后像两边扩散 ...

  6. Linux安装python

    1.打开终端,输入:wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0b4.tgz下载完毕后 2.输入解压命令:tar –zxvf Py ...

  7. cpu 核数及逻辑数统计

    查看逻辑CPU个数:cat /proc/cpuinfo |grep "processor"|sort -u|wc -l24 查看物理CPU个数:grep "physica ...

  8. S3C2440 SPI驱动框架

    S3C2440 SPI驱动代码详细解读: https://www.linuxidc.com/Linux/2012-08/68402p4.htm 一.platform device and board_ ...

  9. 解决jenkins拉取报告没有样式的问题(还没试过)

    来源: https://testerhome.com/topics/9476 对于测试报告来说,除了内容的简洁精炼,样式的美观也很重要.常用的做法是,采用HTML格式的文档,并搭配CSS和JS,实现自 ...

  10. Vue.js:template

    ylbtech-Vue.js: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   1 ...