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. cocos2d-x基础元素之显示对象

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  2. JFrame小练习1

    1.文本域组件 public class TestJTextArea { public static void main(String[] args) { JFrame jf=new JFrame(& ...

  3. 对Jena的简单理解和一个例子

    本文简单介绍Jena(Jena 2.4),使用Protégé 3.1(不是最新版本)创建一个简单的生物(Creature)本体,然后参照Jena文档中的一个例子对本体进行简单的处理,输出本体中的Cla ...

  4. Docker CentOS 7.2镜像systemd问题解决办法

    docker的CentOS 7.2最新版官方镜像使用systemctl管理程序时会遇到如下错误: Failed to get D-Bus connection: Operation not permi ...

  5. [分享]一个String工具类,也许你的项目中会用得到

    每次做项目都会遇到字符串的处理,每次都会去写一个StringUtil,完成一些功能. 但其实每次要的功能都差不多: 1.判断类(包括NULL和空串.是否是空白字符串等) 2.默认值 3.去空白(tri ...

  6. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. SQL 分页查询的四种方法

    方法一 假设现在有这样的一张表: CREATE TABLE test ( id int primary key not null identity, names ) ) 然后向里面插入大约100条数据 ...

  8. Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)

    1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...

  9. Linux与Windows 解压乱码 UTF8BOM读取问题

    Linux 与 Windows 文件乱码问题 这几天需要在linux下用CNN跑数据,但是把数据和数据列表list上传到linux下时却出现了不少乱码的问题.将这两天碰到的编码问题简单的总结一下. 1 ...

  10. java操作excel文件

    采用POI操作excel API:http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html poi包:http ...