2166. Design Bitset Medium

@problem@discussion
#Array#Hash Table#Design



1/**
2 * [2166] Design Bitset
3 *
4 * A Bitset is a data structure that compactly stores bits.
5 * Implement the Bitset class:
6 * 
7 * 	Bitset(int size) Initializes the Bitset with size bits, all of which are 0.
8 * 	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.
9 * 	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.
10 * 	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.
11 * 	boolean all() Checks if the value of each bit in the Bitset is 1. Returns true if it satisfies the condition, false otherwise.
12 * 	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.
13 * 	int count() Returns the total number of bits in the Bitset which have value 1.
14 * 	String toString() Returns the current composition of the Bitset. Note that in the resultant string, the character at the i^th index should coincide with the value at the i^th bit of the Bitset.
15 * 
16 *  
17 * Example 1:
18 * 
19 * Input
20 * ["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
21 * [[5], [3], [1], [], [], [0], [], [], [0], [], []]
22 * Output
23 * [null, null, null, null, false, null, null, true, null, 2, "01010"]
24 * Explanation
25 * Bitset bs = new Bitset(5); // bitset = "00000".
26 * bs.fix(3);     // the value at idx = 3 is updated to 1, so bitset = "00010".
27 * bs.fix(1);     // the value at idx = 1 is updated to 1, so bitset = "01010". 
28 * bs.flip();     // the value of each bit is flipped, so bitset = "10101". 
29 * bs.all();      // return False, as not all values of the bitset are 1.
30 * bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "00101".
31 * bs.flip();     // the value of each bit is flipped, so bitset = "11010". 
32 * bs.one();      // return True, as there is at least 1 index with value 1.
33 * bs.unfix(0);   // the value at idx = 0 is updated to 0, so bitset = "01010".
34 * bs.count();    // return 2, as there are 2 bits with value 1.
35 * bs.toString(); // return "01010", which is the composition of bitset.
36 * 
37 *  
38 * Constraints:
39 * 
40 * 	1 <= size <= 10^5
41 * 	0 <= idx <= size - 1
42 * 	At most 10^5 calls will be made in total to fix, unfix, flip, all, one, count, and toString.
43 * 	At least one call will be made to all, one, count, or toString.
44 * 	At most 5 calls will be made to toString.
45 * 
46 */
47pub struct Solution {}
48
49// problem: https://leetcode.com/problems/design-bitset/
50// discuss: https://leetcode.com/problems/design-bitset/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54struct Bitset {
55        false
56    }
57
58
59/** 
60 * `&self` means the method takes an immutable reference.
61 * If you need a mutable reference, change it to `&mut self` instead.
62 */
63impl Bitset {
64
65    fn new(size: i32) -> Self {
66        
67    }
68    
69    fn fix(&self, idx: i32) {
70        
71    }
72    
73    fn unfix(&self, idx: i32) {
74        
75    }
76    
77    fn flip(&self) {
78        
79    }
80    
81    fn all(&self) -> bool {
82        
83    }
84    
85    fn one(&self) -> bool {
86        
87    }
88    
89    fn count(&self) -> i32 {
90        
91    }
92    
93    fn to_string(&self) -> String {
94        
95    }
96}
97
98/**
99 * Your Bitset object will be instantiated and called as such:
100 * let obj = Bitset::new(size);
101 * obj.fix(idx);
102 * obj.unfix(idx);
103 * obj.flip();
104 * let ret_4: bool = obj.all();
105 * let ret_5: bool = obj.one();
106 * let ret_6: i32 = obj.count();
107 * let ret_7: String = obj.to_string();
108 */
109
110// submission codes end
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn test_2166() {
118    }
119}
120


Back
© 2025 bowen.ge All Rights Reserved.