描写叙述

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

For example,

Given nums = [0, 1, 3] return 2.

Note:

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

解法

方法一:时间复杂度O(n),空间复杂度O(n/32)

传统方法, 同一时候使用位运算压缩空间(int型32位标记32个数字)

//36ms
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size() + 1;
int c = (n >> 5) + 1;
int *p = new int[c];
memset(p, 0, c*sizeof(int));
for (int x : nums) p[x >> 5] |= (1 << x % 32);
for (int i = 0; i<n; i++) if(((p[i >> 5] >> (i % 32)) & 1) == 0) return i;
}
};

方法二:

位运算,将n+1个数字拓展到2^(lg(n)+1)个数。所有异或就可以得到缺失的数字。

时间复杂度O(2^(lg(n)+1)),空间复杂度O(1)

//36ms
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size() + 1;
int ans = 0;
int _pow = (int)pow(2, 1 + (int)(log(n) / log(2)));
for (int x : nums) ans ^= x;
for (int i = n; i < _pow; i++) ans ^= i;
return ans;
}
};

leetcode268:Missing Number的更多相关文章

  1. LeetCode OJ:Missing Number (丢失的数)

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

  2. leetcode解题报告(17):Missing Number

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

  3. LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

    数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. N ...

  4. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  5. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  6. [LintCode] Find the Missing Number 寻找丢失的数字

    Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array. Example G ...

  7. PAT 1144 The Missing Number[简单]

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  8. 一道面试题Lintcode196-Find the Missing Number

    http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...

  9. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

随机推荐

  1. C#实例:Unity依赖注入使用

    http://jingyan.baidu.com/article/c74d6000840b260f6b595d78.html

  2. 关于ES6(ES2015)开发记坑

    ES2015(以下简称ES6)在开发过程中遇到的问题: 1,必须显示声明变量 //es5中可解释为全局变量 a=5; //es6中报错:a is not defined a=5 2,对于递归调用方式必 ...

  3. 《Java编程思想》笔记 第五章 初始化与清理

    1.构造器 因为创建一个类的对象构造器就会自动执行,故初始化某些东西特好 2.方法重载 方法名相同,参数列表不同. 2.1 区分重载方法 方法重载后区别不同方法的就是方法签名 -->参数类型和个 ...

  4. 网络编程学习笔记--1.socket可读可写条件

    转至 :http://blog.csdn.net/majianfei1023/article/details/45788591 socket可读可写条件,经常做为面试题被问,因为它考察被面试者对网络编 ...

  5. k8s的DamonSet使用

    Deployment 部署的副本 Pod 会分布在各个 Node 上,每个 Node 都可能运行好几个副本.DaemonSet 的不同之处在于:每个 Node 上最多只能运行一个副本. 1.Daemo ...

  6. SpringMVC框架入门配置 IDEA下搭建Maven项目(zz)

    SpringMVC框架入门配置 IDEA下搭建Maven项目 这个不错哦 http://www.cnblogs.com/qixiaoyizhan/p/5819392.html

  7. LUA脚本中O(2)级素数查询

    --================================================================================================== ...

  8. Mysql中大表添加索引的办法

    Hash索引与 Btree索引的区别http://database.51cto.com/art/201010/229525.htm Creating Indexes/Sorting on very l ...

  9. sybase ase 重启

    sybase ase 重启 https://blog.csdn.net/davidmeng10/article/details/50344305 https://blog.csdn.net/wengy ...

  10. django static文件的引入方式(转)

    1. 在django project中创建 static文件夹 2.settings.py中配置要在 STATIC_URL = '/static/'  下边 STATICFILES_DIRS = [ ...