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 ...
随机推荐
- BZOJ 1207
1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3089 Solved: 1499[Submit][Statu ...
- request.getRequestDispatcher(url).forward(request, response)
request.getRequestDispatcher().forward(request, response) 意思是将客户端的请求转向到 getRequestDispatcher()方法中参数定 ...
- libev 学习使用
libev 简单的I/O库. a high performance full featured event loop written in c libev 的大小也比 libevent 小得多并且自 ...
- linux+win7双系统重装win7修复grub的办法
本人是debian+win7的双系统, 下面介绍下重装win7的整个过程以及遇到的一些小问题,在查阅相关博客和朋友的帮助下成功修复, 记录下以便以后有不时之需, 也希望能帮助到遇到同样问题的朋友! 首 ...
- PowerDesigner用例图展示设置
powerdesigner用例图显示设置 powerdesigner中的绘图功能真是不敢恭维,折线半天弄不直,直线半天弄不弯. 1.修改显示设置 tools-->display preferen ...
- 51nod 更难的矩阵取数问题(动态规划)
更难的矩阵取数问题 给定一个m行n列的矩阵,矩阵每个元素是一个正整数,你现在 在左上角(第一行第一列),你需要走到右下角(第m行,第n列),每次只能朝右或者下走到相邻的位置,不能走出矩阵.然后再从右下 ...
- [BZOJ 1912] patrol 巡逻
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1912 Algorithm: K=0:res=(n-1)*2 每条边恰好走2遍 K=1 ...
- [BZOJ 2006] 超级钢琴
Link: https://www.lydsy.com/JudgeOnline/problem.php?id=2006 Algorithm: 对于此类区间最值类问题,我们可以通过控制一端不变来寻找当前 ...
- bzoj DZY Loves Math V
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 509 Solved: 284[Submit][Status][Discuss] Descriptio ...
- POJ 3608 Bridge Across Islands (旋转卡壳)
[题目链接] http://poj.org/problem?id=3608 [题目大意] 求出两个凸包之间的最短距离 [题解] 我们先找到一个凸包的上顶点和一个凸包的下定点,以这两个点为起点向下一个点 ...