关于解决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 ...
随机推荐
- Python:Day41 http、css
HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. 2.请求协议 请求协议的格式如下: 请求首行: // ...
- 网站建设部署与发布--笔记4-部署mysql
部署MySQL 1.更新操作系统 $ yum update -y 2.安装mysql数据库,在CentOS 7.2 中,使用了mariadb替代了官方的mysql $ yum install mari ...
- WiFi-ESP8266入门http(3-3)网页认证上网-post请求-ESP8266程序
第一版 原型系统 连上西电的网 直接发送上网的认证信息 返回认证结果网页 成功上网 #include <ESP8266WiFi.h> #define Use_Serial Serial s ...
- Ubuntu下的 PPPoE 拨号上网方法
1. 配置 pppoe $ sudo pppoeconf 2. 联网 $ sudo pon dsl-provider 3. 断网 $ sudo poff 4. 查看日志 $ plog 5. 查看接口信 ...
- 外部python脚本调用django 手动清理session
调试orm 在django项目根目录下创建文件test_orm.py,它和manage.py是同级的 import os if __name__ == "__main__": # ...
- nginx 配sorry page - error page
include conf.d/*.conf; server { listen 9999; server_name 127.0.0.1; location / { root html; index er ...
- 【原创】研发应该懂的binlog知识(下)
引言 这篇是<研发应该懂的binlog知识(上)>的下半部分.在本文,我会阐述一下binlog的结构,以及如何使用java来解析binlog. 不过,话说回来,其实严格意义上来说,研发应该 ...
- FineUIPro v3.5.0发布了,减少 90% 的上行数据量,15行代码全搞定!
一切为客户着想 一切的一切还得从和一位台湾客户的沟通说起: 客户提到将ViewState保存在服务器端以减少上行数据量,从而加快页面的回发速度. 但是在FineUI中,控件状态都保存在FState中, ...
- OpenResty入门之使用Lua扩展Nginx
记住一点:nginx配置文件很多坑来源自你的空格少了或多了. 1.Centos下载安装 如果你的系统是 Centos 或 RedHat 可以使用以下命令: yum install readline-d ...
- Spring+SpringMVC+Mybatis框架整合流程
一:基本步骤 新建Maven项目,导入相关依赖.(推荐) ————–Mybatis配置 —————- 新建entity包,并根据数据库(表)新建相关实体类. 新建dao包,并根据业务创建必要的mapp ...