leetcode268:Missing Number
描写叙述
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的更多相关文章
- LeetCode OJ:Missing Number (丢失的数)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- leetcode解题报告(17):Missing Number
描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...
- 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 ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- [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 ...
- PAT 1144 The Missing Number[简单]
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- 一道面试题Lintcode196-Find the Missing Number
http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
随机推荐
- 《Java编程思想》笔记 第六章 访问权限控制
1.编译单元 一个 编译单元即 .java 文件 内只能有一个 public 类 且该文件名必须与public 类名 完全一致. 编译单元内也可以没有public类 文件名可随意. 2. 包:库单元 ...
- ModelMap和ModelAndView区别
首先介绍ModelMap和ModelAndView的作用 ModelMap ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可 ...
- zabbix通过snmp监控vmware vpshere5.5
https://www.iyunv.com/thread-516343-1-1.html
- Linux下安装Sybase ASE 16
https://jingyan.baidu.com/article/414eccf67281a16b421f0a76.html
- 详解cookie与session的区别,讲得最透彻的一篇文章
在PHP面试中 经常碰到请阐述session与cookie的区别与联系,以及如何修改两者的有效时间. 大家都知道,session是存储在服务器端的,cookie是存储在客户端的,session依赖于c ...
- 组合数学+错排问题【p4071】[SDOI2016]排列计数
Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...
- 51nod 循环数组最大子段和(动态规划)
循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出 输 ...
- 学习LSM(Linux security module)之二:编写并运行一个简单的demo
各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...
- RPD Volume 168 Issue 4 March 2016 评论4
Non-vascular interventional procedures: effective dose to patient and equivalent dose to abdominal o ...
- SD 一轮集训 day1 carcar
可以发现每条边只能选一次或者两次,并且最后每个点的度数(∑邻接边选的次数和)都是偶数(代表有欧拉回路). 然后根据题意列一个 n 行 m+1 列的01矩阵,每一行代表一个异或方程组(每个点的度数是偶数 ...