位图一

来源:互联网 发布:软件可靠性定义 编辑:程序博客网 时间: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;};
原创粉丝点击