位图一
来源:互联网 发布:软件可靠性定义 编辑:程序博客网 时间:2024/06/06 09:20
面试题:给40亿个不重复的无符号整数,没排过序。给定一个无符号整数,如何快速判断一个数是否在这40亿个数中
分析:
40亿个数,4 000 000 000相当于4G个数,每个数4个字节,总共16G
- 采用遍历的方法,运行内存为4G,数据不能全部加载到内存,所以这个方法不行;
- 采用哈希的方法,虽然效率高,但是空间浪费大;
- 采用位图的方法,用字节0或1代表这个数是否存在,整数的范围从0~2^32-1,需要有2^32-1大约有40亿个比特位;2^32/2^3 = 2^29,需要有512M的空间,我们利用512M的空间就可以判断出这个整数是否在这40亿个数中;
实现位图结构的封装代码:
#include<iostream>#include<vector>#include<assert.h>using namespace std;//位图//面试题:给40亿个不重复的无符号整数,没排过序。给定一个无符号整数,如何快速判断一个数是否在这40亿个数中template<size_t N>//N代表比特位数class BitSet{public: BitSet() { _s.resize( (N >> 5) + 1); } BitSet(unsigned long value)//将value的比特位拷贝进_s中 { _s.push_back(value); } BitSet(const string& s) { //判断字符串是否符合要求,即只有0或则1 for (size_t idx = 0; idx < s.size(); ++idx) { if ('0' != s[idx] && '1' != s[idx]) { cout << "输入的字符串不和法" << endl; return; } } _s.resize((N >> 5) + 1); for (size_t idx = 0; idx < N; ++idx) { if (s[idx] == '1') { Set(idx); } } } void Set(size_t whichBit)//whichBit位置置1 { assert(whichBit < N); size_t index = whichBit / 32;//index表示_s对应的下标 size_t pos = whichBit % 32;//pos表示对用字节中对应的比特位(从右向左) _s[index] = _s[index] | (1 << pos); } void reSize(size_t whichBit)//whichBit的位置上置0 { assert(whichBit < N); size_t index = whichBit/32; size_t pos = whichBit % 32; _s[index] &= ~(1 << pos); } int& operator[](size_t whichBit) { assert(whichBit < N); size_t index = whichBit / 32; size_t pos = whichBit % 32; return _s[index] & (~(1 << pos)); } size_t Size() { return N; } size_t Count()//位图中比特位为1的个数 { char* strBitOfHex = "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"; size_t count = 0; for (size_t idx = 0; idx <= N / 32; ++idx) { size_t value = _s[idx];//十六进制,计算数字中1的个数,例如“0x12345678” size_t i = sizeof(char); while (i--) { char charValue = value;//4字节中取最低的1个字节,8位“78” count += strBitOfHex[charValue & 0x0F];//计算低4位1的个数,‘8’的1个个数为1 charValue = charValue >> 4;//右移4个比特位,取到‘7’,1的个数为3 count += strBitOfHex[charValue & 0x0F]; } } return count; } bool Test(size_t whichBit) { assert(whichBit < N); size_t index = whichBit / 32; size_t pos = whichBit % 32; return (1==_s[index] & (1 << pos)); } bool any() { for (size_t idx = 0; idx < N / 32; ++idx) { int pos = sizeof(int); while(pos--) { if ((_s[idx] & (1 >> pos) )== '0') return false; } } return true; }private: vector<int> _s;};
阅读全文
0 0
- 位图一
- 位图(BMP)文件格式(一)
- 位图图像操作基础(一)
- 编程珠玑(一)位图
- BMP位图格式详解<一>
- Android 位图(一) BitmapFactory类
- 编程珠玑(一):位图排序
- 位图初级(一)——位图的存储
- 位图
- 位图
- 位图
- 位图
- 位图
- 位图
- 位图
- 位图
- 位图
- 位图
- spring配置文件xsd报错的解决方法
- JAVA
- 算法第4版(谢路云译)学习笔记(4) -- 二分法查找算法的分析解释
- 互联网、因特网、万维网、局域网、广域网的区别
- 第五章 初始化与清理
- 位图一
- Lua 元表(metatable)与元方法(metamethod)
- initializationError [Runner:JUnit 4]:报错问题
- 做开发对产品的理解 -- 1
- bash变量
- 通俗易懂详解iptables
- Java 8 将List转换为Map
- 鼠标移入显示二维码(原生、JQURY两种方式)
- Linux下搭建集群环境(3)-----------linux下安装tomcat