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即可,参见代码如下:

解法一:

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 寻找丢失的数字的更多相关文章

  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. DPSR随手笔记

    降质模型 MAP:

  2. CStringArray error C2248: 'CObject::CObject' : cannot access private member declared in class

    在开发中将一个字符串分割,并将子字符串保存在CStringArray中,专门写了一个函数,如下: SplitStringToCString(CString str, TCHAR tszSplit, C ...

  3. linux环境中安装NRPE插件执行远程"本地资源"检查?NRPE安装?

    需求描述: 在安装完nagios之后,需要对本地资源进行监控,比如磁盘空间的使用,进程数,swap空间,等等.这些都不是通过网络提供出来的, 所以,都是本地资源,可以通过NRPE插件实现在客户端中采集 ...

  4. 【转载】springboot + swagger

    注:本文参考自 http://www.jianshu.com/p/0465a2b837d2 https://www.cnblogs.com/java-zhao/p/5348113.html swagg ...

  5. 自定义tag标签-实现long类型转换成Date类型

    数据库里存储的是bigint型的时间,entity实体中存放的是long类型的标签,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个 fmt:formatDa ...

  6. 找不同diff-打补丁patch

    Q:为什么要找不同,为什么要打补丁? A: 在Linux应用中,作为DBA,我们知道MySQL跑在Linux系统之上,数据库最重要的追求就是性能,“稳”是重中之重,所以不能动不动就是换系统或是换这换那 ...

  7. SpringBoot(八)-- 日志

    一.介绍 SpringBoot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logback.如 ...

  8. Kafka(三)-- Kafka主要参数

    原文地址:http://debugo.com/kafka-params/ ############################# System ########################## ...

  9. CopyTransform

    // TransformCopier.cs v 1.1 // homepage: http://wiki.unity3d.com/index.php/CopyTransform using Unity ...

  10. radio的取值

    <dd id="pingjia${evaluation.orderItemId }" class="ms-wf clearfix" idx="$ ...