Design Bitset LeetCode solution in C++
1class Bitset {2public:3vector<int> arr;4int cnt, cntflip;5Bitset(int size) {6arr.resize(size, 0);7cnt = 0, cntflip = 0;8}9void fix(int idx) {10// means current bit is 0 ,so set it to 111if ((arr[idx] + cntflip) % 2 == 0) {12arr[idx]++;13cnt++;14}15}16void unfix(int idx) {17// means current bit is 1,so set it to 018if ((arr[idx] + cntflip) % 2 != 0) {19arr[idx]--;20cnt--;21}22}23void flip() {24// cnt will flip ,if we flip all the bits25cnt = arr.size() - cnt;26cntflip++;27}28bool all() {29if (cnt == arr.size()) return true;30return false;31}32bool one() {33if (cnt >= 1) return true;34return false;35}36int count() {37return cnt;38}39string toString() {40string ans;41for (auto &ele : arr) {42if ((cntflip + ele) % 2 == 0)43ans.push_back('0');44else45ans.push_back('1');46}47return ans;48}49};