Title: #2166 - Design Bitset - LeetCode JavaScript Solutions
Open Graph Title: #2166 - Design Bitset - LeetCode JavaScript Solutions
X Title: #2166 - Design Bitset - LeetCode JavaScript Solutions
Description: A Bitset is a data structure that compactly stores bits. Implement the Bitset class: Bitset(int size) Initializes the Bitset with size bits...
Open Graph Description: A Bitset is a data structure that compactly stores bits. Implement the Bitset class: Bitset(int size) Initializes the Bitset with size bits...
X Description: A Bitset is a data structure that compactly stores bits. Implement the Bitset class: Bitset(int size) Initializes the Bitset with size bits...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/design-bitset
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/design-bitset","name":"#2166 - Design Bitset - LeetCode JavaScript Solutions","description":"A Bitset is a data structure that compactly stores bits. Implement the Bitset class: Bitset(int size) Initializes the Bitset with size bits...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} size\n */\nvar Bitset = function(size) {\n this.bits = new Uint8Array(size);\n this.ones = 0;\n this.flipped = false;\n};\n\n/**\n * @param {number} idx\n * @return {void}\n */\nBitset.prototype.fix = function(idx) {\n if (this.flipped) {\n if (this.bits[idx] === 1) {\n this.bits[idx] = 0;\n this.ones++;\n }\n } else {\n if (this.bits[idx] === 0) {\n this.bits[idx] = 1;\n this.ones++;\n }\n }\n};\n\n/**\n * @param {number} idx\n * @return {void}\n */\nBitset.prototype.unfix = function(idx) {\n if (this.flipped) {\n if (this.bits[idx] === 0) {\n this.bits[idx] = 1;\n this.ones--;\n }\n } else {\n if (this.bits[idx] === 1) {\n this.bits[idx] = 0;\n this.ones--;\n }\n }\n};\n\n/**\n * @return {void}\n */\nBitset.prototype.flip = function() {\n this.flipped = !this.flipped;\n this.ones = this.bits.length - this.ones;\n};\n\n/**\n * @return {boolean}\n */\nBitset.prototype.all = function() {\n return this.ones === this.bits.length;\n};\n\n/**\n * @return {boolean}\n */\nBitset.prototype.one = function() {\n return this.ones > 0;\n};\n\n/**\n * @return {number}\n */\nBitset.prototype.count = function() {\n return this.ones;\n};\n\n/**\n * @return {string}\n */\nBitset.prototype.toString = function() {\n let result = '';\n for (let i = 0; i < this.bits.length; i++) {\n result += this.flipped ? 1 - this.bits[i] : this.bits[i];\n }\n return result;\n};","keywords":"LeetCode 2166, #2166 - Design Bitset, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/design-bitset |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/design-bitset/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/2166-design-bitset.js |
| Array | https://leetcodejavascript.com/tags/array |
| String | https://leetcodejavascript.com/tags/string |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| Design | https://leetcodejavascript.com/tags/design |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow