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. Android开发学习笔记-SharedPreferences的用法

    SharedPreferences介绍:   做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...

  2. 通过MyEclipse部署web应用程序开发环境

    1.下载并安装MyEclipse10 2.配置java开发的jar包 3.配置tomcat服务器 4.部署tomcat服务器 点击Bronse可以查看部署后的文件夹目录 5.启动tomcat服务器 6 ...

  3. linux ss命令使用详解

    ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...

  4. Java 流(Stream)、文件(File)和IO -- Java ByteArrayOutputStream类

    Java ByteArrayOutputStream类 字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中.创建字节数组输出流对象有以下几种方式. 下面的构造 ...

  5. C语言编程规范—命名规则

    C是一门朴素的语言,你使用的命名也应该这样.与Modula-2和Pascal程序员不同,C程序员不使用诸如“ThisVariableIsATemporaryCounter”这样“聪明”的名字.C程序员 ...

  6. Maven update project...后jdk变成1.5,update project后jdk版本改变

    Maven update project...后jdk变成1.5,update project后jdk版本改变 ============================== 蕃薯耀 2018年3月14 ...

  7. Tomcat漏洞利用与安全加固实例分析

    Tomcat中间件经常遇到的漏洞: 1.Tomcat默认存在一个管理后台,默认的管理地址是http://IP或域名:端口号/manager/html 2.Axis2默认口令安全漏洞,默认的管理地址是h ...

  8. 【RF库Collections测试】Set List Value

    Name:Set List ValueSource:Collections <test library>Arguments:[ list_ | index | value ]Sets th ...

  9. AESDK开发之UI消息响应

    UI创建: 在该入口下 case PF_Cmd_PARAMS_SETUP: //.... break; 必须在末尾指定UI数目,UI数目一般是枚举,如果和枚举长度不一致也会报错.所以最好是直接修改枚举 ...

  10. Python进阶 学习笔记(三)

    (涉及内容:定制类) __str__和__repr__ 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__( ...