# 2166.设计位集

class Bitset {

int* set;

int size;

int zeros;

int ones;

bool reverse;

public:

Bitset(int size) {

set = new int[(size+31)/32]();

this->size = size;

zeros = size;

ones = 0;

reverse = false;

}

~Bitset() {

delete[] set;

}

void fix(int idx) {

int index = idx / 32;

int bit = idx % 32;

if(!reverse) {

if((set[index] & (1 << bit)) == 0) {

ones++;

zeros--;

set[index] |= (1 << bit);

}

} else {

if((set[index] & (1 << bit)) != 0) {

ones++;

zeros--;

set[index] ^= (1 << bit);

}

}

}

void unfix(int idx) {

int index = idx / 32;

int bit = idx % 32;

if(!reverse) {

if((set[index] & (1 << bit)) != 0) {

ones--;

zeros++;

set[index] ^= (1 << bit);

}

} else {

if((set[index] & (1<<bit)) == 0) {

ones--;

zeros++;

set[index] |= (1 << bit);

}

}

}

void flip() {

reverse = !reverse;

int temp = ones;

ones = zeros;

zeros = temp;

}

bool all() {

return ones == size;

}

bool one() {

return ones > 0;

}

int count() {

return ones;

}

string toString() {

string s = "";

for(int i = 0, k = 0, num, sta; i < size; k++) {

num = set[k];

for(int j = 0; j < 32 && i < size; i++, j++) {

sta = (num >> j) & 1;

s += (reverse ? (sta ? "0" : "1") : (sta ? "1" : "0"));

}

}

return s;

}

};

• 18
点赞
• 15
收藏
觉得还不错? 一键收藏
• 0
评论
03-22 825
02-12 322
03-31 103
09-06 693
09-06 967
09-08 861
09-07 1113
09-11 167
09-07 645
09-05 913

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。