算法(2) Find All Numbers Disappeared in an Array
题目:整数数组满足1<=a[i]<=n(n是数组的长度),某些元素出现一次,某些元素出现两次,在数组a[i]中找到【1,n】区间中未出现的数字。比如输入【4,3,2,7,8,2,3,1】,输出【5,6】。时间复杂度要求是O(n),空间复杂度要求O(1)
思路:看许多网友说用set数据结构去做,这就违反了空间复杂度的要求,所以排序还是要本地做的。遍历数组,把这个数字放到它该待的地方:比如a[0]=4,那么我们就把4放到a[3]处,然后把a[3]处的7放到a[6]处,把a[6]处3放在a[2]处,把a[2]处的2放在a[1]处,晕了吧
这样做:
step1: 7,3,2,4,8,2,3,1
step2:3,3,2,4,8,2,7,1
step3:2,3,3,4,8,2,7,1
step4:3,2,3,4,8,2,7,1
step4.1:此时想着交换3和3,但是发现重了,那么3还是待在原位吧,我们就进一步考察a[1]这个位置发现a[1]=2,2就是应该待在这里,不需要调整;接着查看a[2]位置,a[2]=3, 不需要调整;a[3]=4,不需要调整;a[4]=8,需要调整,去看8应该呆的a[7]位置=1需要交换:
step5:3,2,3,4,1,2,7,8
step6:1,2,3,4,3,2,7,8
step6.1:此时想着交换3和a[2]位置的3,但是a[2]=3=3,所以这里也就不做置换咯,直接考察下一个位置a[5]=2;
step6.2:想着交换2和a[1]位置的2,但是a[1] = 2 = 2,所以不需要交换咯;
step6.3:接着下一个位置,a[6] = 7,是7该待的地儿,接着下一个位置;a[8] = 7,也是8该待的地儿;
遍历一圈的数组是:12343278
然后再遍历一圈,找到a[i] != i+1的数组项,就能找到缺省的指i+1咯
答案:
https://github.com/honpey/codebox/blob/master/leetcode/array/p448.cpp
相关:
1<= a[i] <= n; 1至n的数字缺少一个,找出缺少的数字;
总结:
特殊的数组可能会出现O(n)的排序方法
算法(2) Find All Numbers Disappeared in an Array的更多相关文章
- LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)
这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...
- leetcode之Find All Numbers Disappeared in an Array
问题来源:Find All Numbers Disappeared in an Array 很久没有刷题了,感觉大脑开始迟钝,所以决定重拾刷题的乐趣.一开始不要太难,选一些通过率高的题目做,然后就看到 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- setLocale的一个用处
setLocale是C库中的一个设置地域化信息的C函数. 函数原型为: char *setlocale(int category, const char *locale) 参数解释: category ...
- 【TOJ 5103】Electric Fence(皮克定理)
描述 In this problem, `lattice points' in the plane are points with integer coordinates. In order to c ...
- 将hexo放到github仓库上
完成了hexo的安装后, 我们只能在本地访问. 因此我们将它放到github上, 方便我们随时随地的用网址访问. 在Blog文件夹目录下输入: cnpm install --save hexo-dep ...
- 构建高可靠hadoop集群之1-理解hdfs架构
本文主要参考 http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html 主要内容是对该文 ...
- virtual box 故障修复
vmware ,virtual box等虚拟化环境为一台系统同时允许运行多台系统成为可能准备了技术支持. 通过软件化的平台虚构出硬件设备的驱动,可谓虚拟化技术应用非常广泛. 在平常的虚拟机启动过程中经 ...
- Hadoop(19)-MapReduce框架原理-Combiner合并
1. Combiner概述 2. 自定义Combiner实现步骤 1). 定义一个Combiner继承Reducer,重写reduce方法 public class WordcountCombiner ...
- python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集
题目内容: 输入两个字符串,输出两个字符串集合的并集. 为保证输出结果一致,请将集合内元素排序之后再输出, 如对于集合aset,可输出sorted(aset). 输入格式: 共两行,每一行为一个字符串 ...
- elasticsearch搜索引擎搭建
在该路径下,运行elasticsearch.bat该命令,后面访问127.0.0.1:9200 出现如下界面说明启动成功 elasticsearch-head操作elasticsearch的图形界面, ...
- DedeCMS V5.7sp2最新版本parse_str函数SQL注入漏洞
织梦dedecms,在整个互联网中许多企业网站,个人网站,优化网站都在使用dede作为整个网站的开发架构,dedecms采用php+mysql数据库的架构来承载整个网站的运行与用户的访问,首页以及栏目 ...
- spring配置jackson不返回null值
#json不返回null spring.jackson.default-property-inclusion=non_null