448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数
[抄题]:
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] 每个数字处理一次。没有被处理的正数就是被前面的挤兑了。背吧
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 要做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]中的缺失数的更多相关文章
- 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 ...
- 448 Find All Numbers Disappeared in an Array 找到所有数组中消失的数字
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字.您能在不使用 ...
- LeetCode 448. Find All Numbers Disappeared in an Array找到所有数组中消失的元素
题目 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您能 ...
- 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 = ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【MFC】MoveWindow();函数使用详解
摘自:http://blog.csdn.net/yjkwf/article/details/5281207 CWnd::MoveWindow 详解 2010-02-02 16:08 3432人阅读 评 ...
- 关于list.extend(iterable)
extend内的参数只要是iterable就可以,那么也可以添加定制的iterable,开整. class A(object): def __init__(self): self.a = 0 def ...
- 处理get中文乱码
package com.servlet; import java.io.IOException; import java.io.PrintWriter; ...
- UGUI 分页渐变居中效果
代码相当冗长,仅作自己记录 在此分页上修改的https://blog.csdn.net/qinyuanpei/article/details/49781133 using UnityEngine;us ...
- lvs之 lvs+nginx+tomcat_1、tomcat_2+redis(lvs dr 模式)
前提:已经安装好 lvs+nginx+tomcat_1.tomcat_2+redis环境 ,可参考 (略有改动,比如tomcat_1.tomcat_2安装在两台机器上,而不是单机多实例 ,自行稍稍变动 ...
- mybatis-generator的坑
有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...
- [Client] looks like we got no XML document in....
无wsdl方式应用webservice时,服务端包含了 include_once'inc/utility_all.php '一直报[Client] looks like we got no XML d ...
- 在laravel中使用QrCode包生成二维码
http://laravelacademy.org/post/2605.html 一切都是按照文档上的说明操作,没有问题
- java输出数组中出现的次数最多的那个及次数
总结:理解每一个定义的变量 package com.aini; //一组数据,要求统计出最大值出现了多少次 public class Xd { public static void main(Stri ...
- Zabbix自定义监控网站服务是否能够正常响应
监测tcp连接数文件名: /etc/zabbix/zabbix_agentd.conf.d/count_tcp.conf UserParameter=count.tcp,netstat -s|g ...