Skip to content

Commit a946ddb

Browse files
committed
leetcode
1 parent f773508 commit a946ddb

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

DesignHashSet/design_hashSet.dart

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
3+
4+
-* Design HashSet *-
5+
6+
7+
Design a HashSet without using any built-in hash table libraries.
8+
9+
Implement MyHashSet class:
10+
11+
void add(key) Inserts the value key into the HashSet.
12+
bool contains(key) Returns whether the value key exists in the HashSet or not.
13+
void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.
14+
15+
16+
Example 1:
17+
18+
Input
19+
["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
20+
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
21+
Output
22+
[null, null, null, true, false, null, true, null, false]
23+
24+
Explanation
25+
MyHashSet myHashSet = new MyHashSet();
26+
myHashSet.add(1); // set = [1]
27+
myHashSet.add(2); // set = [1, 2]
28+
myHashSet.contains(1); // return True
29+
myHashSet.contains(3); // return False, (not found)
30+
myHashSet.add(2); // set = [1, 2]
31+
myHashSet.contains(2); // return True
32+
myHashSet.remove(2); // set = [1]
33+
myHashSet.contains(2); // return False, (already removed)
34+
35+
36+
Constraints:
37+
38+
0 <= key <= 106
39+
At most 104 calls will be made to add, remove, and contains.
40+
41+
42+
*/
43+
44+
import 'dart:typed_data';
45+
46+
class MyHashSet {
47+
final Int32List mp;
48+
MyHashSet() : mp = Int32List(125001);
49+
50+
void add(int key) {
51+
final int index = key ~/ 32;
52+
final int bitPosition = key % 32;
53+
mp[index] |= (1 << bitPosition);
54+
}
55+
56+
void remove(int key) {
57+
final int index = key ~/ 32;
58+
final int bitPosition = key % 32;
59+
mp[index] &= ~(1 << bitPosition);
60+
}
61+
62+
bool contains(int key) {
63+
final int index = key ~/ 32;
64+
final int bitPosition = key % 32;
65+
return (mp[index] & (1 << bitPosition)) != 0;
66+
}
67+
}
68+
69+
/**
70+
* Your MyHashSet object will be instantiated and called as such:
71+
* MyHashSet obj = MyHashSet();
72+
* obj.add(key);
73+
* obj.remove(key);
74+
* bool param3 = obj.contains(key);
75+
*/
76+
77+
/*
78+
79+
80+
81+
82+
83+
84+
*/

DesignHashSet/design_hashSet.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
type MyHashSet struct {
4+
mp []uint32
5+
}
6+
7+
func Constructor() MyHashSet {
8+
return MyHashSet{
9+
mp: make([]uint32, 125001),
10+
}
11+
}
12+
13+
func (this *MyHashSet) Add(key int) {
14+
index := key / 32
15+
bitPosition := uint32(key % 32)
16+
this.mp[index] |= (1 << bitPosition)
17+
}
18+
19+
func (this *MyHashSet) Remove(key int) {
20+
index := key / 32
21+
bitPosition := uint32(key % 32)
22+
this.mp[index] &= ^(1 << bitPosition)
23+
}
24+
25+
func (this *MyHashSet) Contains(key int) bool {
26+
index := key / 32
27+
bitPosition := uint32(key % 32)
28+
return (this.mp[index] & (1 << bitPosition)) != 0
29+
}

DesignHashSet/design_hashSet.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 🔥 Design HashSet - Bit Manipulation 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Explanation
4+
5+
In this optimized version, we use a single Int32List instead of a boolean array. Each element of the list represents 32 bits, allowing us to store the state of multiple keys in a single integer value. We perform bit manipulation operations to set or unset specific bits in the integer.
6+
7+
The add method sets the bit at the corresponding index and position using bitwise OR (|). The remove method un-sets the bit using bitwise AND (&) with the complement of the bit mask. The contains method checks if the bit is set using bitwise AND (&).
8+
9+
This optimized implementation reduces memory usage and provides faster performance for operations on large sets of integers.
10+
11+
Please note that the size of the Int32List is set to accommodate integers up to 1,000,000 (32 * 125001). You can adjust the size based on your specific needs.
12+
13+
1. The MyHashSet class represents a HashSet implementation using bit manipulation.
14+
2. The mp variable is an Int32List used to store the state of the keys.
15+
3. In the constructor MyHashSet(), we initialize mp with a size of 125001, which allows us to store 1,000,000 bits (32 bits per element * 125001 elements).
16+
4. The add method takes an integer key. It calculates the index and bit position of the key in mp. It then uses the bitwise OR (|) operation to set the corresponding bit at the calculated position.
17+
5. The remove method is similar to add. It calculates the index and bit position of the key and uses the bitwise AND (&) operation with the complement of the bit mask to unset the bit at the position.
18+
6. The contains method checks if the bit at the index and position is set. It performs the bitwise AND (&) operation with the bit mask and returns true if the result is non-zero, indicating that the bit is set.
19+
20+
### Space Complexity
21+
22+
The space complexity of the MyHashSet class is O(1) because the size of the Int32List (mp) is fixed at 125001, regardless of the number of elements added.
23+
24+
#### Time Complexity
25+
26+
The time complexity of the add, remove, and contains methods is O(1) because the calculations and bitwise operations performed are constant-time operations.
27+
The optimized implementation using bit manipulation reduces memory usage and provides efficient constant-time operations for adding, removing, and checking the presence of keys in the HashSet.
28+
29+
### Solution - 1 Bit Manipulation
30+
31+
```dart
32+
import 'dart:typed_data';
33+
34+
class MyHashSet {
35+
final Int32List mp;
36+
MyHashSet() : mp = Int32List(125001);
37+
38+
void add(int key) {
39+
final int index = key ~/ 32;
40+
final int bitPosition = key % 32;
41+
mp[index] |= (1 << bitPosition);
42+
}
43+
44+
void remove(int key) {
45+
final int index = key ~/ 32;
46+
final int bitPosition = key % 32;
47+
mp[index] &= ~(1 << bitPosition);
48+
}
49+
50+
bool contains(int key) {
51+
final int index = key ~/ 32;
52+
final int bitPosition = key % 32;
53+
return (mp[index] & (1 << bitPosition)) != 0;
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)