[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) [ ...
随机推荐
- 元素随屏幕滚动到顶部固定js效果
网站中常见这种效果,某个广告或详情页切换tab,当屏幕向下移动时,该元素会停留在浏览器最顶部,下面ecshop模板中心教您实现js代码: 案例图: 1.首先在页面上找到该元素 加上 id =&quo ...
- ThreadLocal用法
使用ThreadLocal能实现线程级别的变量定义,同一个类的私有静态变量,在不同的线程中值可以不同. 1.学习文章:http://blog.csdn.net/qjyong/article/detai ...
- 小企业是否能用得上"ITIL"?
在小型IT部门中,明显存在着迫切的IT管理需求.但目前主流ITSM解决方案的价格.实施周期.复杂程度.对人力资源的占用等使他们难以承受. 浦发机械公司的计算机部经理老张带着十几个员工,经过数年 ...
- MySQL自定义排序函数FIELD()
MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1.str2.str3.str4比较,返回1.2.3.4,如遇 ...
- 基金、社保和QFII等机构的重仓股排名评测
来源:基金前20大重仓股持仓股排名 基金前15大重仓股持仓股排名 基金重仓前15大个股,相较于同期沪深300的平均收益,近1月:-1.05%,近3月:-0.49%,近6月:1.45%,近1年:3.92 ...
- docker dockerfile构建自己的tomcat镜像
文件 1.apache-tomcat-8.5.24.tar.gz, jdk-8u151-linux-x64.tar.gz Dockerfile文件: FROM centosWORKDIR /usrCO ...
- Eclipse------用Tomcat运行项目后出现:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
Eclipse中Tomcat运行项目后出现: 严重: Error configuring application listener of class org.springframework.web.c ...
- java之 ------ 可变參数和卫条件
可变參数:适用于參数个数不确定.类型确定的情况,java把可变參数当做数组处理. 可变參数必须位于最后一项.当可变參数个数多于一个时,必将有一个不是最后一项,所以仅仅支持有一个可变參数. 可变參数的书 ...
- 【RF库Collections测试】Remove Values From List
Name:Remove Values From ListSource:Collections <test library>Arguments:[ list_ | *values ]Remo ...
- Memcached 数据导出与导入
我们使用 memcached-tool 命令来导出数据: [root@localhost ~]# memcached-tool dump > /tmp/.txt Dumping memcache ...