10.3 Given an input file with four billion non-negative integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB of memory available for this task.
FOLLOW UP
What if you have only 10 MB of memory? Assume that all the values are distinct and we now have no more than one billion non-negative integers.

这道题给我们了一个很大很大的数据文件,里面都是有四十亿个非负整数,现在给了我们1GB的内存大小,让我们找到一个不包括在这个文件中的非负整数,我们需要用位向量Bit Vector来做,跟之前那道5.8 Draw Horizonatal Line 画横线有些类似,题目中说数据文件总共有四十亿个数字,也就是232个,那么非负数就有231个,给了我们1GB内存,也就是八十亿位,我们可以把每个整数都映射到内存中的不同位,逻辑如下:

1. 建立一个四十亿位大小的位向量Bit Vector,位向量是一个使用整型数组来存储bool型(或其他数据类型)值的数组,每个整型或bool型占32位。

2. 初始化位向量为0.

3. 遍历所有数字,给每个数字对应的位设为1.

4. 遍历位向量,找到第一个为0的位,算出其对应的整数。

参见代码如下:

class Solution {
public:
void findOpenNumber() {
vector<unsigned char> v(INT_MAX / );
ifstream in("input.txt");
int d;
if (in.is_open()) {
while (in >> d) {
v[d / ] |= << (d % );
}
in.close();
} else cout << "Cannot open file!\n";
for (int i = ; i < v.size(); ++i) {
for (int j = ; j < ; ++j) {
if ((v[i] & ( << j)) == ) {
cout << i * + j << endl;
return;
}
}
}
}
};

这道题有个Follow Up,是说只给了我们10MB的内存大小,问如何解题。那么既然内存有限,我们只能讲大数据拆分为许多小的块Block,比如说每个块大小为1000,所以块0为数字0到999,块1为数字1000到1999等等。这样我们只要先找到是哪个块少了数字,然后再在这个块中具体查找少了哪个数字。下面我们就要来看每个块大小设定为多少,给了我们10MB内存,也就是223个字节,由于一个整型占4个字节,所以最多能有221个元素,所以我们区间大小不能小于231/221=210个,又由于10MB内存,也就是223个字节,共有226位,所以每个块可表示的数字范围可以在210到226之间选,我们选取靠近中间的220作为数字范围,因为越靠近中间,任意时间就会有更少的内存被占用,代码如下:

class Solution {
public:
void findOpenNumber() {
int bitSize = ; // 2^20 bits (2^17 bytes)
int blockNum = ;
vector<unsigned char> v(bitSize / );
vector<int> b(blockNum);
int starting = -;
ifstream in("input.txt");
int d;
if (in.is_open()) {
while (in >> d) {
++b[d / (v.size() * )];
}
in.close();
} else cout << "Cannot open file!\n";
for (int i = ; i < b.size(); ++i) {
if (b[i] < v.size() * ) {
starting = i * v.size() * ;
break;
}
}
ifstream in2("input.txt");
if (in2.is_open()) {
while (in2 >> d) {
if (d >= starting && d < starting + v.size() * ) {
v[(d - starting) / ] |= << ((d - starting) % );
}
}
in2.close();
}
for (int i = ; i < v.size(); ++i) {
for (int j = ; j < ; ++j) {
if ((v[i] & ( << j)) == ) {
cout << i * + j + starting << endl;
return;
}
}
}
}
};

[CareerCup] 10.3 Integer not Contain in the File 文件中不包含的数的更多相关文章

  1. [CareerCup] 10.4 Find All Duplicates Elements 寻找所有的重复项

    10.4 You have an array with all the numbers from 1 to N, where N is at most 32,000. The array may ha ...

  2. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接

    10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...

  3. 4.产生10个1-100的随机数,并放到一个数组中 (1)把数组中大于等于10的数字放到一个list集合中,并打印到控制台。 (2)把数组中的数字放到当前文件夹的numArr.txt文件中

    package cn.it.text; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayLis ...

  4. sort +awk+uniq 统计文件中出现次数最多的前10个单词

    实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

  5. windows phone xaml文件中元素及属性(10)

    原文:windows phone xaml文件中元素及属性(10) Textblock xaml文件和隐藏文件 在设计界面的时候我们可以通过xaml中进行设计,这种设计是所见即所得的,很是方便,由于x ...

  6. 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri

    [源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...

  7. 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容

    [源码下载] 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 作 ...

  8. 转载:Linux命令经典面试题:统计文件中出现次数最多的前10个单词

    1.使用linux命令或者shell实现:文件words存放英文单词,格式为每行一个英文单词(单词可以重复),统计这个文件中出现次数最多的前10个单词 主要考察对sort.uniq命令的使用,相关解释 ...

  9. [CareerCup] 10.1 Client-facing Service 面向客户服务器

    10.1 Imagine you are building some sort of service that will be called by up to 1000 client applicat ...

随机推荐

  1. UVa 112 - Tree Summing(树的各路径求和,递归)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. 调研Android平台的开发环境的发展演变

    ·  安卓是以linux为基础的开放源码操作系统.因为安卓的开源等原因,所以现在市场上会有大量的APP可供使用,且各个方面都功能强大. ·  也许是因为开源的原因,安卓过于碎片化.每个APP互相独立. ...

  3. [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案

    在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...

  4. MFC分类

    屏幕截图(带光标) MFC Button控件自绘 WM_CTLCOLOR消息 MFC窗口创建.销毁消息流程 DDX_Control.SubclassWindow和SubclassDlgItem 隐藏系 ...

  5. 读书笔记——网络编程与开发技术(3)基于TCP/IP协议的网络编程相关知识

    TCP/IP协议:数据链路层,网络层,传输层,应用层. IP地址分为5类:A类.B类.C类.D类.E类. (A类.B类.C类是基本类,D类多用于多播传送,E类为保留类.) "*"表 ...

  6. radclient安装记录

    下载地址: http://freeradius.org/getting.html 选择:1.1.5版本 wget -c ftp://ftp.freeradius.org/pub/freeradius/ ...

  7. Android开发之 Android应用程序详细解析

    我们继续的沿用上一篇所建立的应用. Android应用程序可以分为:应用程序源代码(.java),应用程序描述文件(.xml),各种资源. 可以这么理解: 安卓应用程序,通过java代码来实现其业务逻 ...

  8. MongodbBackup Script

    #!/usr/bin/env python # _*_coding:utf-8_*_ # Author: "Edward.Liu" # Author-Email: lonnyliu ...

  9. 活动与服务onbind()

  10. TestNG之参数化

    TestNG提供了两种参数化的方式,一种是通过XML,一种是通过代码实现,下面对这两种方式做介绍. 一.通过xml /** * <suite name="Suite" par ...