Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

Example 1

Input: [3,0,1]
Output: 2

Example 2

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

给n个数,0到n之间有一个数字缺失了,让找到这个缺失数字。要求线性时间复杂度和常数级的额外空间复杂度。

解法1:排序,然后二分查找。此题数组不是排序的,所以适合。但面试时,如果问道要知道。

解法2:求差值,用0~n个数的和减去给定数组数字的和,差值就是要找的数字。

解法3:位操作Bit Manipulation,将这个数组与0~n之间完整的数异或一下累加到res,相同的数字异或后都变为了0,最后剩下的结果就是缺失的数字。

Java:

class Solution {
public int missingNumber(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int result = 0; Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (result != nums[i]) {
return result;
}
result++;
}
return nums.length;
}
}  

Java:

class Solution {
public int missingNumber(int[] nums) {
int xor = 0, i = 0; for (i = 0; i < nums.length; i++) {
xor = xor ^ i ^ nums[i];
} return xor ^ i;
}
}  

Python:

def missingNumber(self, nums):
n = len(nums)
return n * (n+1) / 2 - sum(nums)  

Python:

class Solution(object):
def missingNumber(self, nums):
return sum(xrange(len(nums)+1)) - sum(nums)

Python:

class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return reduce(operator.xor, nums, \
reduce(operator.xor, xrange(len(nums) + 1)))  

C++: 用等差数列公式

class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum = 0, n = nums.size();
for (auto &a : nums) {
sum += a;
}
return 0.5 * n * (n + 1) - sum;
}
};

C++:

class Solution {
public:
int missingNumber(vector<int>& nums) {
int res = 0;
for (int i = 0; i < nums.size(); ++i) {
res ^= (i + 1) ^ nums[i];
}
return res;
}
};

类似题目:

[CareerCup] 5.7 Find Missing Integer 查找丢失的数

All LeetCode Questions List 题目汇总

[LeetCode] 268. Missing Number 缺失的数字的更多相关文章

  1. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  2. 268 Missing Number 缺失的数字

    给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...

  3. LeetCode 268. Missing Number缺失数字 (C++/Java)

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...

  4. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  5. Java [Leetcode 268]Missing Number

    题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

  6. LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

    1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...

  7. 33. leetcode 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  8. leetcode 268 Missing Number(异或运算的应用)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. Leetcode 268 Missing Number 位运算

    题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...

随机推荐

  1. [AI] 切换cuda版本的万金油

    1. 环境 ubuntu16.04 GTX1080Ti x 4 nvidia-418 cuda-10.1 pytorch1.0.0 目标:在最新的显卡驱动下,使用不同版本的cuda和深度学习框架来执行 ...

  2. ThinkPHP支持的4种路由模式

    下面这个说法和示例,比较具有总结性.

  3. 数据库jdbc链接:mysql, oracle, postgresql

    #db mysql#jdbc.driver=com.mysql.jdbc.Driver#jdbc.url=jdbc:mysql://localhost:3306/mysql?&useUnico ...

  4. 解决:一个项目中写多个包含main函数的源文件并分别调试运行

    自己在学c++的时候,一个项目中的多个cpp文件默认不允许多个main函数的出现,但是通过选项操作能够指定单个cpp文件进行运行,如下: 1.此时我就想运行第二个cpp文件,我们只需要把其他的两个右键 ...

  5. 04-Flutter移动电商实战-打通底部导航栏

    关于界面切换以及底栏的实现可参考之前写的一篇文章:Flutter实 ViewPager.bottomNavigationBar界面切换 1.新建4个基本dart文件 在pages目录下,我们新建下面四 ...

  6. 数论--扩展欧几里得exgcd

    算法思想 我们想求得一组\(x,y\)使得 \(ax+by = \gcd(a,b)\) 根据 \(\gcd(a,b) = \gcd(b,a\bmod b)\) 如果我们现在有\(x',y'\) 使得 ...

  7. 通过granfana 以及prometheus 比较zulu 、oracle、openjdk 等jvm 运行

    说明,此测试不完备,只是一个简单的集成,详细的需要进行jvm 参数的调整 环境准备 参考项目 https://github.com/rongfengliang/zulu-openjdk-openjdk ...

  8. Calibre中使用DeDRM插件进行Kindle电子书解锁

    小书匠 废话不多说,下面是Calibre和DeDRM插件的下载地址: https://calibre-ebook.com/download https://github.com/apprenticeh ...

  9. linux命令之------Tar解压缩

    Tar解压缩 作用:将解压缩后缀名为tar的压缩包 -f<备份文件>或—file=<备份文件>指定备份文件 -v或-verbose显示指令执行过程 -x或-extract或-g ...

  10. 无人机一体化3DGIS服务平台

    随着无人机技术的发展,无人机携带多种设备为GIS应用提供多元化海量基础数据.无人机航测更是以快速.灵活.高效的数据获取方式,迅速扩大了现有的GIS市场,同时GIS行业的广泛应用也推动了无人机技术的发展 ...