forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyHashSet.java
More file actions
83 lines (67 loc) · 2.66 KB
/
Copy pathMyHashSet.java
File metadata and controls
83 lines (67 loc) · 2.66 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
// Time Complexity :
// add(key) -> Average: O(1), Worst: O(n) (when many keys land in same bucket)
// remove(key) -> Average: O(1), Worst: O(n)
// contains(key) -> Average: O(1), Worst: O(n)
// (n = number of elements in the bucket due to collisions)
//
// Space Complexity :
// O(N + B) where N = total keys stored, B = number of buckets (1000)
//
// Did this code successfully run on Leetcode :
// Yes
//
// Any problem you faced while coding this :
// No major issues. The main thing to be careful about is:
// - Initializing the LinkedList bucket only when needed (lazy init)
// - Removing by value using Integer.valueOf(key) so it doesn't treat key as an index
// - Handling negative keys safely using Math.floorMod
// Your code here along with comments explaining your approach
import java.util.LinkedList;
class MyHashSet {
// Number of buckets (fixed size). More buckets => fewer collisions on average.
int parentbuckets;
// Array of buckets; each bucket is a LinkedList to handle collisions (separate chaining).
LinkedList<Integer>[] storage;
public MyHashSet() {
// Chosen bucket size (common for this LeetCode problem).
this.parentbuckets = 1000;
// Create an array of LinkedLists (buckets). Buckets are initialized lazily.
this.storage = new LinkedList[parentbuckets];
}
// Primary hash function: maps the key into a valid bucket index [0..parentbuckets-1]
// floorMod ensures negative keys also map correctly.
private int getPrimaryHash(int key) {
return Math.floorMod(key, parentbuckets);
}
public void add(int key) {
int index = getPrimaryHash(key);
// Lazy bucket creation (saves memory for unused buckets)
if (storage[index] == null) {
storage[index] = new LinkedList<>();
}
// Avoid duplicates: only add if it's not already present
if (!storage[index].contains(key)) {
storage[index].add(key);
}
}
public void remove(int key) {
int index = getPrimaryHash(key);
// If bucket exists, remove the key if present
if (storage[index] != null) {
// Integer.valueOf(key) removes the object, not by index
storage[index].remove(Integer.valueOf(key));
}
}
public boolean contains(int key) {
int index = getPrimaryHash(key);
// Key exists if bucket exists and it contains the key
return storage[index] != null && storage[index].contains(key);
}
}
/**
* Your MyHashSet object will be instantiated and called as such:
* MyHashSet obj = new MyHashSet();
* obj.add(key);
* obj.remove(key);
* boolean param_3 = obj.contains(key);
*/