算法(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 ...
随机推荐
- EF core 中用lambda表达式和Linq的一些区别
转眼一看,又过了10几天没有写博客了,主要还是没有什么可以写的,因为遇到的问题都不是很有价值.不过最近发现用lambda表达式,比用Linq的代码量会少一些,而且也方便一些.不过两者都差不多,相差不是 ...
- frame3.5安装出错
一般是因为禁用了microsoft update,可以在服务里禁用改为手动,之后启动,然后就可以安装
- filter-policy和AS-PATH-FILTER过滤BGP路由条目
Filter-policy过滤BGP路由条目 一:根据项目需求搭建好拓扑图如下: 二:配置 1:对项目图做理论分析,首先RT1和RT2属于EBGP(不同自治系统之间的直连路由),而RT2和RT3属于I ...
- 01-HTML深入
1.1 浏览器的工作原理 把一些标签解析成用户可视化的页面 1.2 HTML中的标签与元素 在HTML中以<xx>开始,以</xx>结束,比如<html>< ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- 文件 I/O字节流
输入字节流: import java.io.*; public class test_main { public static void main(String[] args) { int n=-1; ...
- POJ2553 汇点个数(强连通分量
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12070 Accepted: ...
- 【好帖】 Mark
1. 管理篇 2. 程序员选择公司的8个标准 3. 实用工具 4. 离职跳槽 5. DBA 6. 做一个网站多少钱? 7. 十大算法 8. 寻求用户评价App的正确方法 9. 工程师忽略的隐形成本 1 ...
- ResolutionException: Cannot find candidate artifact for com.google.android.gms:play-services-ads-lite:[10.2.4]
I had the same issue and I think it's solved now. Open AdMobDependencies.cs file, located inside of ...
- 国际电话区号SQL
CREATE TABLE `phone_prefix` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `country` varchar(30) N ...