停止刷题已经三周了,有些想念。最近总算完成了公司代码的重构,于是要继续开始学习算法。

先来看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] return 2.

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之类的算法问题的更多相关文章

  1. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

  2. <LeetCode OJ> 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  3. 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 ...

  4. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  5. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  6. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  7. hdu 5166 Missing number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...

  8. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  9. 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 ...

随机推荐

  1. 深度学习之从RNN到LSTM

    1.循环神经网络概述 循环神经网络(RNN)和DNN,CNN不同,它能处理序列问题.常见的序列有:一段段连续的语音,一段段连续的手写文字,一条句子等等.这些序列长短不一,又比较难拆分成一个个独立的样本 ...

  2. 获取列表的索引操作:enumerate

    通过循环获取列表的索引操作: 主要使用:enumerate product_list = [['Iphone7',5800], ['Coffee',30], ['疙瘩汤',10], ['Python ...

  3. ansible的playbook进行yum批量安装nginx最新版本

    环境:centos7 版本:nginx最新版本 软件: ansible 作用: 进行批量执行不同机器上,进行安装nginx版本 检查脚本是否正确: [root@ansible-test ansible ...

  4. pycharm企业版注册码

    pycharm下载最新版 链接:https://pan.baidu.com/s/1gKOCf3PQFc1_2amkMUU1-A 提取码:9pt0 下载企业版: http://www.jetbrains ...

  5. python:while循环、运算符、初始编码

    while循环 while -- 关键字 while 条件: 缩进代码块 以上循环是(死循环) 终止循环的方法 1.break 跳出循环,并且把循环给干掉了 2.continue 跳出本次循环,继续下 ...

  6. 强大的原生DOM选择器querySelector和querySelectorAll

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

  7. 两个数字比较大小的方法 (分别应用if-else和条件运算符实现)

    package com.Summer_0424.cn; /** * @author Summer * 两个数字比较大小的方法 * 分别应用if-else和条件运算符实现 */ public class ...

  8. 环境部署(三):Linux下安装Git

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理,是目前使用范围最广的版本管理工具. 这篇博客,介绍下Linux下安装Git的步骤,仅供参考,当然,还是yum安装 ...

  9. scrapy 爬取糗事百科

    安装scrapy conda install scrapy 创建scrapy项目 scrapy startproject qiubai 启动pycharm,发现新增加了qiubai这个目录 在spid ...

  10. Grafana+Prometheus打造全方位立体监控系统

    前言 本文主要介绍如何使用Grafana和Prometheus以及node_exporter对Linux服务器性能进行监控.下面两张图分别是两台服务器监控信息: 服务器A 服务器B 概述 Promet ...