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. 列车网络智能诊断工具链—MVB智能诊断仪

    由于MVB网络采用分布式网络结构,各组网设备分布在不同电气柜,甚至是在不同车辆上,各组网设备往往来自于不同供应商,这给MVB网络调试及诊断带来了很大的难度.目前MVB网络调试及故障排查,主要是通过仪器 ...

  2. charAt,charCode,fromCharCode区别

    1.charAt 返回字符串指定位置的字符 2.charCode 返回字符串指定位置字符Unicode编码 3.fromCharCode 用Unicode编码创建字符串 我们来看下例子 var str ...

  3. keras神经网络开发知识笔记

    mnist数据集获取60000个训练样本和10000个测试样本,样本为0-9十个数字,用全连接神经网络进行训练,并测试结果. 例程采用60000个数据样本进行训练,对于一般的电脑来说,这样训练会比较费 ...

  4. django-导入应用包的搜索路径

    创建应用包 在 settings.py注册和配置urls.py中要按顺序导入包名和应用名 settings.py INSTALLED_APPS = ( 'django.contrib.admin', ...

  5. Jmeter+ant+jekins环境配置

    Jmeter+ant+jekins 一.ant安装 1. ant安装 官网下载http://ant.apache.org 解压到想要的盘里面 2. 配置环境变量 (1)变量名:ANT_HOME 变量值 ...

  6. javascript之反柯里化uncurrying

    使用方法: // 使用 var push=Array.prototype.push.uncurrying(); var obj={ "length": 1, "0&quo ...

  7. 洛谷P3620 [APIO/CTSC 2007] 数据备份

    题目 贪心+堆. 一般贪心题用到堆的时候都会存在一种反悔操作,因此这个题也不例外. 首先电缆一定是连接两个相邻的点的,这很好证明,其次一个点只能被一条电缆连接,所以我们通过选这个电缆,不选相邻电缆和选 ...

  8. 微信小程序前端promise封装

    config.js const config = { base_url_api : "https://douban.uieee.com/v2/movie/", } export { ...

  9. mysql lower()函数

    mysql> select " DFREF"; +--------+ | DFREF | +--------+ | DFREF | +--------+ row in set ...

  10. 【cf补题记录】A. Hotelier

    思考之后再看题解,是与别人灵魂之间的沟通与碰撞 A. Hotelier 题意 给出长度为n的字符串,字符串由'L'.'R'以及数字0~9组成.旅馆有10间房子,L代表客人从左边入住,R代表客人从右边入 ...