算法(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 ...
随机推荐
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- ABAP术语-Accounting Document
Accounting Document 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/12/991731.html Accounting d ...
- HDFS的存储策略
本文介绍hdfs的存储策略 内容译自:http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/ArchivalStor ...
- Mac系统配置php环境
[写在前面——叨叨叨] -_-#急着配环境的同志们可以绕道.最近学校的实验室里接了一个小项目——考勤刷卡系统,利用RFID在硬件层获取学生卡的ID,通过wifi传输至服务器,进行考勤信息存储,手机端获 ...
- Graylog2日志服务安装配置
软件版本: mongodb-org-3.2.10 jdk-1.8.0 (推荐rpm包,不然要修改Graylog启动脚本定义的JAVA命令路径) elasticsearch-2.4.1 (Graylog ...
- pip安装拓展包--网络超时/Read timed out问题
pip安装拓展包--网络超时/Read timed out问题 解决方案:切换镜像源(墙皮太厚) 在后面加上: -i https://pypi.douban.com/simple example: p ...
- 来自一枚初生牛犊不怕虎的小菜鸟的Mock.js使用,不足之处欢迎读者的指出 谢谢
本文章写的是基于require的mock.js的几种常用生成随机数据和ajax模拟前后端的交互信息 <script src="./app/libs/require.js"&g ...
- Python数值
一.python数值类型 python数值类型有以下四种: int(整数) float(浮点数) complex(复数) bool(布尔型) 注意:python3取消了long型. 二.python ...
- u-boot、kernel、root系统烧写和挂载命令命令
一.uboot 环境变量: 1. 打印环境变量:# print 2. 设置启动参数# set bootargs noinitrd init=/linuxrc console=ttySAC0,11520 ...
- Linux 内核之api_man 手册安装
开发环境:Ubuntu18.04,虚拟机virtual box 1.安装XML格式转换 sudo apt install xmlto 2.在内核目录执行 make mandocs 大概持续了半小时 ...