forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashSet.java
More file actions
68 lines (59 loc) · 2.02 KB
/
HashSet.java
File metadata and controls
68 lines (59 loc) · 2.02 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
// Time Complexity :O(1)
// Space Complexity :O(primaryBuckets × secondaryBuckets)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : I had some issues with the syntax other than that i am good
/**
* 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);
*/
// Your code here along with comments explaining your approach
// for add function, i checked if there are any elements in that index in 1st array if not initialized an array and kept that particular element in its hashed index
// for remove, and contain- i checked whether that index is there is or not id there i removed it, and for contain i return true/false
class MyHashSet {
int primaryBuckets;
int secondaryBuckets;
boolean[][] storage;
public MyHashSet() {
this.primaryBuckets = 1000;
this.secondaryBuckets = 1000;
this.storage = new boolean[primaryBuckets][];
}
public int hash1(int key){
return key%primaryBuckets;
}
public int hash2(int key){
return key/secondaryBuckets;
}
public void add(int key) {
int hash_1 = hash1(key);
int hash_2 = hash2(key);
if (storage[hash_1] == null){
if (hash_1 == 0){
storage[hash_1] = new boolean[secondaryBuckets+1];
}
else{
storage[hash_1] = new boolean[secondaryBuckets];
}
}
storage[hash_1][hash_2] = true;
}
public void remove(int key) {
int hash_1 = hash1(key);
int hash_2 = hash2(key);
if(storage[hash_1] == null){
return;
}
storage[hash_1][hash_2] = false;
}
public boolean contains(int key) {
int hash_1 = hash1(key);
int hash_2 = hash2(key);
if (storage[hash_1] == null){
return false;
}
return storage[hash_1][hash_2];
}
}