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. jvm 堆栈概念

    关于JVM的工作原理以及调优是一个向往已久的模块,终于有幸接触到:http://pengjiaheng.iteye.com/blog/518623 那就顺着这个思路,来梳理一下自己看到后的结论和感想. ...

  2. Codeforces1114F Please, another Queries on Array?

    题目链接:http://codeforces.com/problemset/problem/1114/F 题意:序列$a$,有两种操作,1 区间里的数同时乘上$x$ 2 求区间的积的欧拉函数 线段树好 ...

  3. 六.深浅copy

    先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...

  4. 学习Spring-Data-Jpa(十三)---动态查询接口JpaSpecificationExecutor

    1.JpaSpecificationExecutor JPA2引入了一个criteria API,我们可以使用它以编程的形式构建查询.通过编写criteria,动态生成query语句.JpaSpeci ...

  5. LeetCode 845. Longest Mountain in Array

    原题链接在这里:https://leetcode.com/problems/longest-mountain-in-array/ 题目: Let's call any (contiguous) sub ...

  6. LeetCode 1102. Path With Maximum Minimum Value

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-minimum-value/ 题目: Given a matrix of integer ...

  7. LOJ P10130 点的距离 题解

    这道题相当于倍增求LCA的板子,我们只要构建一棵树,然后距离就是x的深度+y的深度 - LCA(x,y)的深度: #include<iostream> #include<cstdio ...

  8. PDO和MySQLi区别和数度;到底用哪个?

    当用PHP访问数据库时,除了PHP自带的数据库驱动,我们一般还有两种比较好的选择:PDO和MySQLi.在实际开发过程中要决定选择哪一种首先要对二者有一个比较全面的了解.本文就针对他们的不同点进行分析 ...

  9. 守护进程daemon.c

    它的特点是:•不占用控制终端(后台运行)•独立于控制终端•周期性运行 #include<stdio.h>#include<unistd.h>#include<fcntl. ...

  10. Go内置函数

    append go语言中的append函数作用是在切片变量的后面追加新的数据,然后返回新的切片变量 func append(slice []Type, elems ...Type) []type sl ...