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 ...
随机推荐
- 驱动模块和装模块的概念——Junit单元测试案例
驱动模块是用来模拟被测试模块的上一级模块,相当于被测模块的主程序.它接收数据,将相关数据传送给被测模块,启用被测模块,并打印出相应的结果. 桩模块(Stub)是指模拟被测试的模块所调用的模块,而不是软 ...
- servletcontext.getRealPath()
getRealPath方法已经不建议使用了: http://blog.csdn.net/lzynihao/article/details/8315796 另外:http://veryls.iteye. ...
- MATLAB多项式及多项式拟合
多项式均表示为数组形式,数组元素为多项式降幂系数 1. polyval函数 求多项式在某一点或某几个点的值. p = [1,1,1];%x^2+x+1 x = [-1,0,1];y = po ...
- C# axWindowsMediaPlayer制作播放器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- (转)Docker 基础 : Dockerfile
全文来自 Docker 基础 : Dockerfile Dockerfile 是一个文本格式的配置文件,用户可以使用 Dockerfile 快速创建自定义的镜像.我们会先介绍 Dockerfile 的 ...
- HDU1009:FatMouse' Trade(初探贪心,wait)
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- 37、Django实战第37天:404以及500页面配置
1.把404.html,500.html复制到templates下,替换静态文件路径 2.编辑users.views.py定义404,505函数 from django.shortcuts impor ...
- 棋盘V
问题 A: 棋盘V 时间限制: 1 Sec 内存限制: 128 MB提交: 150 解决: 3[提交] [状态] [讨论版] [命题人:] 题目描述 有一块棋盘,棋盘的边长为100000,行和列的 ...
- 对mysql 数据库操作 使其支持插入中文(针对python)
首先,这项任务确切的说需要三步吧: #1.建立数据库(数据库名为xsk) create database `xsk` character set 'utf8' collate 'utf8_genera ...
- jboss启动时java VM参数设置
jboss服务器中jvm参数的设置: 在$JBOSS_HOME/bin下的run.sh里面存在这么一个设置: # Force IPv4 on Linux systems since IPv6 does ...