题目:

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

Example 1:

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

Example 2:

  1. Input: [9,6,4,2,3,5,7,0,1]
  2. 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++

  1. //use math
  2. class Solution {
  3. public:
  4. int missingNumber(vector<int>& nums) {
  5. int n = nums.size();
  6. int res = (+n)*n/;
  7. for(int q:nums)
  8. res -= q;
  9. return res;
  10. }
  11. };
  12. //use xor
  13. class Solution {
  14. public:
  15. int missingNumber(vector<int>& nums) {
  16. int res = nums.size();
  17. for(int i = ; i < nums.size(); ++i){
  18. res = res ^ i ^ nums[i];
  19. }
  20. return res;
  21. }
  22. };

Java

  1. class Solution {
  2. public int missingNumber(int[] nums) {
  3. int n = nums.length;
  4. int res = (1+n)*n/2;
  5. for(int q:nums)
  6. res -= q;
  7. return res;
  8. }
  9. }
  10. class Solution {
  11. public int missingNumber(int[] nums) {
  12. int res = nums.length;
  13. for(int i = 0; i < nums.length; ++i){
  14. res = res ^ i ^ nums[i];
  15. }
  16. return res;
  17. }
  18. }

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. acwing 7 混合背包

    习题地址  https://www.acwing.com/problem/content/description/7/ 题目描述有 N 种物品和一个容量是 V 的背包. 物品一共有三类: 第一类物品只 ...

  2. 2019-2020-1 20199305《Linux内核原理与分析》第六周作业

    系统调用的三层机制(下) (一)给MenuOS增加命令 (1)打开虚拟机,首先用rm -rf menu指令删除当前的menu目录,然后用git clone重新克隆一个新版本的menu,进入menu,运 ...

  3. 剑指Offer-31.整数中1出现的次数(从1到n整数中1出现的次数)(C++/Java)

    题目: 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...

  4. 【LOJ6397】「THUPC2018」蛋糕 / Cake(搜索)

    点此看题面 大致题意: 把一个\(a\times b\times c\times d\)的\(4\)维图形划分成\(a\times b\times c\times d\)个小块,求有\(0\sim8\ ...

  5. 【STM32H7教程】第31章 STM32H7的USART应用之RS485

    完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第31章       STM32H7的USART应用之RS48 ...

  6. Docker学习——Dockerfile

    上一篇我们讲了docker的基本使用,掌握了前一篇,docker使用基本不成问题,但是要是你学习了Dockerfile,你会发现它使用起来有多方便了.项目最终部署时,我们希望docker容器打开时项目 ...

  7. WPF 使用SetParent嵌套窗口

    有点类似与Winform的MDI窗口. 使用函数为SetParent和MoveWindow(经常配合). [DllImport("user32.dll", SetLastError ...

  8. python爬取小说

    运行结果: 代码: import requests from bs4 import BeautifulSoup from selenium import webdriver import os cla ...

  9. TreeViewItem节点添加图标后再加header

    1.需要实现的效果如图 2.解决方案 1).给TreeViewItem中添加children,children为包含一个stackpanel,在stackpanel中包含Image和TextBlock ...

  10. php中搭建Web服务器和服务器配置

    1.搭建Web服务器     1.1目录结构 1.2访问服务器 访问规则:http://服务器ip地址/php页面 比如: http://localhost/demo.php http://127.0 ...