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

先来看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. 定义静态map

    public final static Map<String, String> header = new HashMap<String, String>(); static { ...

  2. remix的使用

    remix首先,这个东西其实是有一个线上版本的,只要登录上网址:https://remix.ethereum.org就可以直接使用了,但是我更多用的是本地配置的remix-ideremix-ide的文 ...

  3. Spring Security(二十五):7. Sample Applications

    There are several sample web applications that are available with the project. To avoid an overly la ...

  4. Spring Security(十八):5.9 Post Processing Configured Objects

    Spring Security’s Java Configuration does not expose every property of every object that it configur ...

  5. mysql远程连接 Host * is not allowed to connect to this MySQL server

    mysql -u root -p mysql>use mysql; mysql>update user set host =’%'where user =’root’; mysql> ...

  6. ASP.NET Core 添加区域步骤(详细)

    1 前言 早就想总结一下,但是没时间,这次有时间了,就详细的把步骤写出来. 2 步骤 2.1 添加区域 右键项目->添加->区域,如图1: 区域名称:Ceshi 添加完之后解决方案管理器会 ...

  7. 解决Android Studio 错误方法

    https://blog.csdn.net/lang523493505/article/details/82914253 https://blog.csdn.net/qq_23599965/artic ...

  8. RNG牛掰!

    2018-05-21 RNG牛掰!Uzi圆梦! 不说了,先去哭了! 2018-07-08 洲际赛后更新,RNG依然牛逼! 2018-08-30 亚运后后更新,UZI加油! 2018-10-22 继续加 ...

  9. ajax成功后XML 解析错误:格式不佳

    就是Ajax发送请求后,意图回显数据时会出现这个错误,貌似chrome浏览器不会报用火狐能看到: 可能的原因有两个,就是后台应该返回一个json格式的字符串,但是你返回的是浏览器看不懂的,也就是返回格 ...

  10. Python学习第四篇——列表访问与判定

    avilable_foods=["soup","beaf","noddle","pepper"] request_foo ...