Given an array contains N numbers of 0 .. N, find which number doesn't exist in the array.

Example

Given N = 3 and the array [0, 1, 3], return 2.

Challenge

Do it in-place with O(1) extra memory and O(n) time.

这道题是LeetCode上的原题,请参见我之前的博客Missing Number 丢失的数字。那道题用了两种方法解题,但是LintCode的OJ更加严格,有一个超大的数据集,求和会超过int的范围,所以对于解法一的话需要用long来计算数组之和,其余部分都一样,记得最后把结果转成int即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: a vector of integers
  5. * @return: an integer
  6. */
  7. int findMissing(vector<int> &nums) {
  8. // write your code here
  9. long sum = , n = nums.size();
  10. for (auto &a : nums) {
  11. sum += a;
  12. }
  13. return (int)(n * (n + ) * 0.5 - sum);
  14. }
  15. };

用位操作Bit Manipulation和之前没有区别,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: a vector of integers
  5. * @return: an integer
  6. */
  7. int findMissing(vector<int> &nums) {
  8. // write your code here
  9. int res = ;
  10. sort(nums.begin(), nums.end());
  11. for (int i = ; i < nums.size(); ++i) {
  12. res ^= nums[i] ^ (i + );
  13. }
  14. return res;
  15. }
  16. };

[LintCode] Find the Missing Number 寻找丢失的数字的更多相关文章

  1. 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 ...

  2. 【zz】面试题之寻找丢失的数字

    据传说是MS/Google等等IT名企业的面试题: 有一组数字,从1到n,中减少了一个数,顺序也被打乱,放在一个n-1的数组里 请找出丢失的数字,最好能有程序,最好算法比较快 BTW1: 有很多种方法 ...

  3. Missing number

    Missing number 题目: Description There is a permutation without two numbers in it, and now you know wh ...

  4. 一道面试题Lintcode196-Find the Missing Number

    http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...

  5. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

  6. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  7. hdu 5166 Missing number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5166 Missing number Description There is a permutatio ...

  8. Missing Number, First Missing Positive

    268. Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find th ...

  9. HDU 5166 Missing number 简单数论

    Missing number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) [ ...

随机推荐

  1. 元素随屏幕滚动到顶部固定js效果

    网站中常见这种效果,某个广告或详情页切换tab,当屏幕向下移动时,该元素会停留在浏览器最顶部,下面ecshop模板中心教您实现js代码: 案例图: 1.首先在页面上找到该元素  加上 id =&quo ...

  2. ThreadLocal用法

    使用ThreadLocal能实现线程级别的变量定义,同一个类的私有静态变量,在不同的线程中值可以不同. 1.学习文章:http://blog.csdn.net/qjyong/article/detai ...

  3. 小企业是否能用得上"ITIL"?

    在小型IT部门中,明显存在着迫切的IT管理需求.但目前主流ITSM解决方案的价格.实施周期.复杂程度.对人力资源的占用等使他们难以承受.     浦发机械公司的计算机部经理老张带着十几个员工,经过数年 ...

  4. MySQL自定义排序函数FIELD()

    MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1.str2.str3.str4比较,返回1.2.3.4,如遇 ...

  5. 基金、社保和QFII等机构的重仓股排名评测

    来源:基金前20大重仓股持仓股排名 基金前15大重仓股持仓股排名 基金重仓前15大个股,相较于同期沪深300的平均收益,近1月:-1.05%,近3月:-0.49%,近6月:1.45%,近1年:3.92 ...

  6. docker dockerfile构建自己的tomcat镜像

    文件 1.apache-tomcat-8.5.24.tar.gz, jdk-8u151-linux-x64.tar.gz Dockerfile文件: FROM centosWORKDIR /usrCO ...

  7. 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 ...

  8. java之 ------ 可变參数和卫条件

    可变參数:适用于參数个数不确定.类型确定的情况,java把可变參数当做数组处理. 可变參数必须位于最后一项.当可变參数个数多于一个时,必将有一个不是最后一项,所以仅仅支持有一个可变參数. 可变參数的书 ...

  9. 【RF库Collections测试】Remove Values From List

    Name:Remove Values From ListSource:Collections <test library>Arguments:[ list_ | *values ]Remo ...

  10. Memcached 数据导出与导入

    我们使用 memcached-tool 命令来导出数据: [root@localhost ~]# memcached-tool dump > /tmp/.txt Dumping memcache ...