关于解决Missing Number之类的算法问题
停止刷题已经三周了,有些想念。最近总算完成了公司代码的重构,于是要继续开始学习算法。
先来看leetcode上面第268题:
Given an array containing n distinct numbers taken from
0, 1, 2, ..., n, find the one that is missing from the array.For example,
Given nums =[0, 1, 3]return2.Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
这个题写的是给你一个排序好的数组,然后你需要找到这个数组里面缺的那个数字,而且需要使用线性复杂度O(n)和constant extra space常数级别的空间O(1)。
这类题要做成O(n)的时间复杂度其实都是一个思路,就是使用异或进行两两抵消。异或及是一种对位相加不进位的操作,比如5 和 3 进行异或就是 0101 和0011是 0110就是6。相同的按位与为0 不同的按位与为1。
这道题只需要制造一个n长度的队列,然后依次与given_nums 与,最后剩下的数就是缺少的数:
xor = 0
i = 0
pipi = [0, 1, 3, 4] for i in range(len(pipi)):
xor = xor ^ i ^ pipi[i]
i += 1
print xor ^ i
就可以得到结果2。
类似的思路其实还可以解不少题。例如给你一堆成对的数 只落单了一个数让你找出他。
例如给你一个p = [5, 4, 3, 2, 2, 3, 5] 少了一个4 让你把他用O(n)的复杂度 把他找出来就非常适用于这种方法。
两两按位与之后就会得到4. 所以可以总结出一个公式 0 = X XOR X
以上。
关于解决Missing Number之类的算法问题的更多相关文章
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- hdu 5166 Missing number
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...
随机推荐
- Oracle hint之ORDERED和USE_NL
Hint:ORDERED和USE_NL ORDERED好理解,就是表示根据 from 后面表的顺序join,从左到右,左边的表做驱动表 use_nl(t1,t2):表示对表t1.t2关联时采用嵌套循环 ...
- P1024 一元三次方程求解(二分答案)
思路: 求这个根,然后有一个关键的条件|x1-x2|>=1,然后就是从-100,枚举到+100,每次二分(i, i+1)注意如果f(i)*f(i+1)>0则不进行二分,如果,你觉得这样的值 ...
- 【转】Windows中设置Fiddler抓HTTPS请求的解决办法 Unable to configure Windows to Trust the Fiddler Root certificate .
官网设置 Click Tools > Fiddler Options > HTTPS. Click the Decrypt HTTPS Traffic box. 按照上述要求,我的设置是这 ...
- jenkins忘记管理员密码之解决方案
jenkins忘记管理员密码怎么办? 通常有这么几种解决方案,如下所示: (1)进入对应的用户目录文件夹,以ubuntu16.04为例,jenkins安装目录为/var/lib/jenkins进入到该 ...
- 【CTF杂项】常见文件文件头文件尾格式总结及各类文件头
文件头文件尾总结 JPEG (jpg), 文件头:FFD8FF 文件尾:FF D9PNG (png), 文件头:89504E47 文件尾:AE 42 60 82GIF (gif), 文件头:47494 ...
- nginx + tomcat = http && https
Tomcat版块配置: vim /to/path/conf/server.xml <Server port="" shutdown="SHUTDOWN"& ...
- linux内存源码分析 - 内存池
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 内存池是用于预先申请一些内存用于备用,当系统内存不足无法从伙伴系统和slab中获取内存时,会从内存池中获取预留的 ...
- 用C# 模拟实现unity里的协程
注:需要了解C#的迭代器,不然很难理解. 之前面试有被问到unity协程的原理,以及撇开unity用纯C#去实现协程的方法.后来了解一下,确实可以的.趁这会有空,稍微总结一下. 还是结合代码说事吧: ...
- Ext中 grid 设置行样式
//设置样式 JS var SetRowClass = function(record, rowIndex, rowParams, store) { if (record.data.status ...
- Java 将两个Map对象合并为一个Map对象
实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式. publi ...