Skip to content

Commit df36e50

Browse files
authored
Update 2166-design-bitset.js
1 parent 2738583 commit df36e50

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

2166-design-bitset.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,86 @@
1+
/**
2+
* @param {number} size
3+
*/
4+
const Bitset = function (size) {
5+
this.arr = Array.from({ length: 2 }, (el, idx) =>
6+
Array(size).fill(idx === 0 ? 0 : 1)
7+
)
8+
this.cur = 0
9+
this.cnt = 0
10+
}
11+
12+
/**
13+
* @param {number} idx
14+
* @return {void}
15+
*/
16+
Bitset.prototype.fix = function (idx) {
17+
if(this.arr[this.cur][idx] === 1) return
18+
this.arr[this.cur][idx] = 1
19+
this.arr[this.cur ^ 1][idx] = 0
20+
this.cnt++
21+
}
22+
23+
/**
24+
* @param {number} idx
25+
* @return {void}
26+
*/
27+
Bitset.prototype.unfix = function (idx) {
28+
if(this.arr[this.cur][idx] === 0) return
29+
this.arr[this.cur][idx] = 0
30+
this.arr[this.cur ^ 1][idx] = 1
31+
this.cnt--
32+
}
33+
34+
/**
35+
* @return {void}
36+
*/
37+
Bitset.prototype.flip = function () {
38+
this.cur ^= 1
39+
this.cnt = this.arr[this.cur].length - this.cnt
40+
}
41+
42+
/**
43+
* @return {boolean}
44+
*/
45+
Bitset.prototype.all = function () {
46+
return this.cnt === this.arr[this.cur].length
47+
}
48+
49+
/**
50+
* @return {boolean}
51+
*/
52+
Bitset.prototype.one = function () {
53+
return this.cnt > 0
54+
}
55+
56+
/**
57+
* @return {number}
58+
*/
59+
Bitset.prototype.count = function () {
60+
return this.cnt
61+
}
62+
63+
/**
64+
* @return {string}
65+
*/
66+
Bitset.prototype.toString = function () {
67+
return this.arr[this.cur].join('')
68+
}
69+
70+
/**
71+
* Your Bitset object will be instantiated and called as such:
72+
* var obj = new Bitset(size)
73+
* obj.fix(idx)
74+
* obj.unfix(idx)
75+
* obj.flip()
76+
* var param_4 = obj.all()
77+
* var param_5 = obj.one()
78+
* var param_6 = obj.count()
79+
* var param_7 = obj.toString()
80+
*/
81+
82+
// another
83+
184
/**
285
* @param {number} size
386
*/

0 commit comments

Comments
 (0)