描写叙述

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. k8s的deployment应用

    Kubernetes 通过各种 Controller 来管理 Pod 的生命周期.为了满足不同业务场景,Kubernetes 开发了 Deployment.ReplicaSet.DaemonSet.S ...

  2. 微信token

    <?php define("TOKEN", "lmaster"); function checkSignature() { //从GET参数中读取三个字段 ...

  3. (2)Python 变量和运算符

    一.python变量特点 python是弱类型语言,无需声明变量可以直接使用并且变量的数据类型可以动态改变 二.变量命名规则 1.不能使用python关键字 2.不能数字开头 3.不能包含空格 4.不 ...

  4. HDU 5969 最大的位或【贪心/按位或/思维】

    链接 最大的位或 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  5. python——入门系列(一)索引与切片

    1.索引和切片:python当中数组的索引和其他语言一样,从0~n-1,使用索引的方法也是中括号,但是python中的切片的使用简化了代码 索引:取出数组s中第3个元素:x=s[2] 切片:用极少的代 ...

  6. 并查集【CF731C】Socks

    Description 有\(n\)只袜子,\(k\)种颜色,在\(m\)天中,问最少修改几只袜子的颜色,可以使每天要穿的袜子颜色相同. Input 第一行\(n,m,k\)分别对应题目描述. 接下来 ...

  7. 循环节(BFS)

    循环节 时间限制: 1 Sec  内存限制: 64 MB提交: 56  解决: 16[提交][状态][讨论版] 题目描述 第一节是英语课.今天,老师又教了桐桐很多单词.桐桐发现所有单词都有循环节(大写 ...

  8. luogu P4012 深海机器人问题

    luogu P4012 深海机器人问题 // luogu-judger-enable-o2 #include<queue> #include<cstdio> #include& ...

  9. URL 和URI的区别

    1.统一资源标志符URI就是在某一规则下能把一个资源独一无二地标识出来. URL:(Uniform/Universal Resource Locator 的缩写,统一资源定位符). URI:(Unif ...

  10. Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dua ...