题目:

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?

分析:

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

最先想到将数组进行排序,再依次判定数和索引是否对应,首次不对应的索引便是缺失的数。不过由于需要对数组进行排序,这样时间复杂度就不会是线性的了。

也可以创建一个集合,将数组中元素添加进集合中,再从0到n对集合进行查询,不在集合内的便是缺失的数。不过这样空间复杂度就不是常数级的了。

我们知道所给的数的序列是从0到n,且只缺失一个数,根据高斯求和公式,可以快速求得0到n这n+1个数的和,再减去序列中的n个数,剩下的便是缺失的数。

还可以利用异或运算(XOR),我们知道对一个数进行两次完全相同的异或运算会得到原来的数,即

a ^ b ^ b = a

a ^ a = 0

根据题目我们知道,未缺失的数,在[0-n]和数组中各出现了一次,而缺失的数只在[0-n]中出现了一次,根据这个条件和异或的特性可以通过一次循环求得缺失数。

假设所给的序列为0,1,3,4,我们可以求得

missing = 4 ^ 0 ^ 0 ^ 1 ^ 1 ^ 2 ^ 3 ^ 3 ^ 4

     = (4 ^ 4) ^ (0 ^ 0) ^ (1 ^ 1) ^ 2 ^ (3 ^ 3)

     = 0 ^ 0 ^ 0 ^ 0 ^ 2

     = 2

程序:

C++

//use math
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int res = (+n)*n/;
for(int q:nums)
res -= q;
return res;
}
};
//use xor
class Solution {
public:
int missingNumber(vector<int>& nums) {
int res = nums.size();
for(int i = ; i < nums.size(); ++i){
res = res ^ i ^ nums[i];
}
return res;
}
};

Java

class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int res = (1+n)*n/2;
for(int q:nums)
res -= q;
return res;
}
}
class Solution {
public int missingNumber(int[] nums) {
int res = nums.length;
for(int i = 0; i < nums.length; ++i){
res = res ^ i ^ nums[i];
}
return res;
}
}

LeetCode 268. Missing Number缺失数字 (C++/Java)的更多相关文章

  1. [LeetCode] 268. Missing Number 缺失的数字

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

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

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

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

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

  4. 268 Missing Number 缺失的数字

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

  5. 【LeetCode】268. Missing Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...

  6. Java [Leetcode 268]Missing Number

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

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

  8. 33. 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(异或运算的应用)

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

随机推荐

  1. Node.js接口避免重复启动

    众所周知,一个Node接口要是想被调用,得先在命令行中执行如下代码来启动接口 node base.js 但是一旦修改了base.js,就得重新执行这句命令 注:这里的base.js是我的node接口文 ...

  2. Re-爬楼梯

    题目地址 https://dn.jarvisoj.com/challengefiles/CFF_100.rar.dbeee1536c0a5ef5844f42c93602aae5 看看功能,看样子要爬到 ...

  3. AcWing 795. 前缀和

    题目地址  https://www.acwing.com/solution/AcWing/content/2075/ 题目描述输入一个长度为n的整数序列. 接下来再输入m个询问,每个询问输入一对l, ...

  4. AcWing 803. 区间合并

    网址 https://www.acwing.com/solution/AcWing/content/1590/ 题目描述给定n个区间[l, r]. 合并所有有交集的区间. 输出合并完成后的区间个数. ...

  5. AcWing 33. 链表中倒数第k个节点

    习题地址 https://www.acwing.com/solution/acwing/content/2997/ 题目描述输入一个链表,输出该链表中倒数第k个结点. 注意: k >= 0;如果 ...

  6. Java Web 学习(7) —— Spring MVC 之国际化

    Spring MVC 之国际化 i18n 与 l10n internationalization:国际化,以 i 开头,以 n 结尾,中间 18 个字母,简称 i18n. localization:本 ...

  7. 如何让junit的测试跑多次

    对JUnit4可以使用下面的方法: @RunWith(Parameterized.class) public class RunTenTimes { @Parameterized.Parameters ...

  8. python执行cmd命令

    os.system os.system用来执行cmd指令,在cmd输出的内容会直接在控制台输出,返回结果为0表示执行成功 注意:os.system是简单粗暴的执行cmd指令,如果想获取在cmd输出的内 ...

  9. Spring Batch基本概念

    Spring batch主要有以下部分组成: JobRepository     用来注册job的容器 JobLauncher             用来启动Job的接口 Job           ...

  10. Java设计模式:Prototype(原型)模式

    概念定义 使用原型实例指定待创建对象的种类,并通过拷贝该原型来创建新的对象.Prototype模式允许一个原型对象克隆(复制)出多个与其相同的对象,而无需知道任何如何创建的细节. 应用场景 对象的创建 ...