[LintCode] Find the Missing Number 寻找丢失的数字
Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array.
Given N = 3 and the array [0, 1, 3], return 2.
Do it in-place with O(1) extra memory and O(n) time.
这道题是LeetCode上的原题,请参见我之前的博客Missing Number 丢失的数字。那道题用了两种方法解题,但是LintCode的OJ更加严格,有一个超大的数据集,求和会超过int的范围,所以对于解法一的话需要用long来计算数组之和,其余部分都一样,记得最后把结果转成int即可,参见代码如下:
解法一:
class Solution {
public:
/**
* @param nums: a vector of integers
* @return: an integer
*/
int findMissing(vector<int> &nums) {
// write your code here
long sum = , n = nums.size();
for (auto &a : nums) {
sum += a;
}
return (int)(n * (n + ) * 0.5 - sum);
}
};
用位操作Bit Manipulation和之前没有区别,参见代码如下:
解法二:
class Solution {
public:
/**
* @param nums: a vector of integers
* @return: an integer
*/
int findMissing(vector<int> &nums) {
// write your code here
int res = ;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); ++i) {
res ^= nums[i] ^ (i + );
}
return res;
}
};
[LintCode] Find the Missing Number 寻找丢失的数字的更多相关文章
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- 【zz】面试题之寻找丢失的数字
据传说是MS/Google等等IT名企业的面试题: 有一组数字,从1到n,中减少了一个数,顺序也被打乱,放在一个n-1的数组里 请找出丢失的数字,最好能有程序,最好算法比较快 BTW1: 有很多种方法 ...
- Missing number
Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...
- 一道面试题Lintcode196-Find the Missing Number
http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...
- Leetcode-268 Missing Number
#268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- hdu 5166 Missing number
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...
- Missing Number, First Missing Positive
268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...
- HDU 5166 Missing number 简单数论
Missing number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) [ ...
随机推荐
- linux环境中安装ftp服务
需求说明: 今天项目中有一个新的需求,需要在linux环境中搭建一个ftp服务,在此记录下. 操作过程: 1.通过yum的方式安装ftp服务对应的软件包 [root@testvm01 ~]# yum ...
- springboot+jps+druid项目搭建
pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- 5 -- Hibernate的基本用法 --4 6 Hibernate事务属性
事务也是Hibernate持久层访问的重要方面,Hibernate不仅提供了局部事务支持,也允许使用容器管理的全局事务. Hibernate关于事务管理的属性: ⊙ hibernate.transac ...
- trim思考
今天发现后台订单商品名称没有的时候出现了HTML代码,然后看了一下源代码(下图是简化版本的) <?php $name = trim('<span style="font-weig ...
- 在SELECT DISTINCT 状况下使用 Order BY Newid() 随机数选出记录
在日常作业中,有时候可能是一些活动要抽出得奖人或选出抽查的一些名单, 就常常会使用到 Order BY Newid() 的方式来做随机数选出, 但有可能的状况需是要搭配到 DISTINCT 来选出,这 ...
- Memcached 数据导出与导入
我们使用 memcached-tool 命令来导出数据: [root@localhost ~]# memcached-tool dump > /tmp/.txt Dumping memcache ...
- 使用 Splash
Splash 简介与安装 Splash Lua 脚本 Splash 对象属性 Splash 对象方法 Splash API 调用 Splash 负载均衡
- Ajax 结果提取
Python 如何提取 Ajax 真正响应的内容: 以 https://m.weibo.cn/u/2830678474 这个网页为例,选择其中一个 Ajax 请求,找到请求的URL和传递的参数 imp ...
- c语言学习笔记---预编译
专题三: 1) 预编译 处理所有的注释,以空格代替, 将所有的#define删除,并且展开所有的宏定义, 处理条件编译指令#if,#ifdef,#elif,#else,#endif 处理# ...
- java里面的getAttribute和findAttribute的区别
findAttribute: abstract Object findAttribute(String name) Searches for the named attribute in page, ...