关于解决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 ...
随机推荐
- css样式的书写顺序及原理
刚开始学习前端的时候,每次写css样式都是用到什么就在样式表后添加什么,完全没有考虑到样式属性的书写顺序对网页加载代码的影响.后来逐渐才知道正确的样式顺序不仅易于查看,并且也属于css样式优化的一种方 ...
- 通过JS如何获取IP地址
通过JS获取你真实的外网IP和内网IP,就算开代理也没有用,想想真是太可怕了,还能不能愉快的装逼了! 代码: //get the IP addresses associated with an acc ...
- Doc2vec实现原理
论文来源:https://www.eecs.yorku.ca/course_archive/2016-17/W/6412/reading/DistributedRepresentationsofSen ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- Linux内存管理之mmap详解
转发之:http://blog.chinaunix.net/uid-26669729-id-3077015.html Linux内存管理之mmap详解 一. mmap系统调用 1. mmap系统调用 ...
- Nginx(二)------nginx.conf 配置文件
上一篇博客我们将 nginx 安装在 /usr/local/nginx 目录下,其默认的配置文件都放在这个目录的 conf 目录下,而主配置文件 nginx.conf 也在其中,后续对 nginx 的 ...
- Elicpse使用技巧-打开选中文件文件夹或者包的当前目录
很多时候,我们需要在eclipse那里打开选中文件(文件夹,包)的当前目录,在资源管理器那里显示这个目录,这个时候,我们又不想采用“选中文件/文件夹/包名--右击--Properties--Locat ...
- [Spark][Flume]Flume 启动例子
Flume 启动例子: flume-ng agent --conf /etc/flume-ng/conf --conf-file /etc/flume-ng/conf/flume.conf --nam ...
- 我的微软最有价值专家(Microsoft MVP)之路
一.写在前面 2018年对我来说是幸运的一年,对我来说最幸运的事情有两个,一个是在离驾照考试过期还有一个月(报名之后一直没去考)终于拿到了我的驾照,还有一件事莫过于获得了微软MVP.期间,一直有朋友问 ...
- 抛弃配置后的Spring终极教程
一:前言 Spring 有XML配置和注解两种版本,我个人非常喜欢使用注解,相当热衷Spring boot! 对于Spring,核心就是IOC容器,这个容器说白了就是把你放在里面的对象(Bean)进行 ...