-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2166-design-bitset.js
More file actions
109 lines (101 loc) · 2.58 KB
/
2166-design-bitset.js
File metadata and controls
109 lines (101 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* 2166. Design Bitset
* https://leetcode.com/problems/design-bitset/
* Difficulty: Medium
*
* A Bitset is a data structure that compactly stores bits.
*
* Implement the Bitset class:
* - Bitset(int size) Initializes the Bitset with size bits, all of which are 0.
* - void fix(int idx) Updates the value of the bit at the index idx to 1. If the value was
* already 1, no change occurs.
* - void unfix(int idx) Updates the value of the bit at the index idx to 0. If the value
* was already 0, no change occurs.
* - void flip() Flips the values of each bit in the Bitset. In other words, all bits with
* value 0 will now have value 1 and vice versa.
* - boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it
* satisfies the condition, false otherwise.
* - boolean one() Checks if there is at least one bit in the Bitset with value 1. Returns
* true if it satisfies the condition, false otherwise.
* - int count() Returns the total number of bits in the Bitset which have value 1.
* - String toString() Returns the current composition of the Bitset. Note that in the
* resultant string, the character at the ith index should coincide with the value at
* the ith bit of the Bitset.
*/
/**
* @param {number} size
*/
var Bitset = function(size) {
this.bits = new Uint8Array(size);
this.ones = 0;
this.flipped = false;
};
/**
* @param {number} idx
* @return {void}
*/
Bitset.prototype.fix = function(idx) {
if (this.flipped) {
if (this.bits[idx] === 1) {
this.bits[idx] = 0;
this.ones++;
}
} else {
if (this.bits[idx] === 0) {
this.bits[idx] = 1;
this.ones++;
}
}
};
/**
* @param {number} idx
* @return {void}
*/
Bitset.prototype.unfix = function(idx) {
if (this.flipped) {
if (this.bits[idx] === 0) {
this.bits[idx] = 1;
this.ones--;
}
} else {
if (this.bits[idx] === 1) {
this.bits[idx] = 0;
this.ones--;
}
}
};
/**
* @return {void}
*/
Bitset.prototype.flip = function() {
this.flipped = !this.flipped;
this.ones = this.bits.length - this.ones;
};
/**
* @return {boolean}
*/
Bitset.prototype.all = function() {
return this.ones === this.bits.length;
};
/**
* @return {boolean}
*/
Bitset.prototype.one = function() {
return this.ones > 0;
};
/**
* @return {number}
*/
Bitset.prototype.count = function() {
return this.ones;
};
/**
* @return {string}
*/
Bitset.prototype.toString = function() {
let result = '';
for (let i = 0; i < this.bits.length; i++) {
result += this.flipped ? 1 - this.bits[i] : this.bits[i];
}
return result;
};