forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.java
More file actions
87 lines (73 loc) · 2.25 KB
/
Copy pathHashMap.java
File metadata and controls
87 lines (73 loc) · 2.25 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
84
85
86
87
// Time Complexity : amortized O(1).
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : We used linear chaining technique here to implement hashset where in we have array of nodes
// as storage buckets. As it's linear chaining we used one hash function used for the array reference and then iterate through the linkedList
// for the operations.
// Your code here along with comments explaining your approach
class MyHashMap {
Node[] storage;
int buckets;
public MyHashMap() {
this.storage = new Node[1000];
this.buckets = 1000;
}
class Node{
int key;
int value;
Node next;
public Node(int key, int value){
this.key = key;
this.value = value;
}
}
private int getHash(int key){
return key%buckets;
}
private Node getPrev(Node head, int key){
Node prev = null;
Node curr = head;
while(curr != null && curr.key != key){
prev = curr;
curr = curr.next;
}
return prev;
}
public void put(int key, int value) {
int index = getHash(key);
if(storage[index] == null){
storage[index] = new Node(-1, -1);
storage[index].next = new Node(key, value);
return;
}
Node prev = getPrev(storage[index], key);
if(prev.next == null){
prev.next = new Node(key, value);
} else{
prev.next.value = value;
}
}
public int get(int key) {
int index = getHash(key);
if(storage[index] == null) return -1;
Node prev = getPrev(storage[index], key);
if(prev.next == null) return -1;
return prev.next.value;
}
public void remove(int key) {
int index = getHash(key);
if(storage[index] == null) return;
Node prev = getPrev(storage[index], key);
if(prev.next == null) return;
Node curr = prev.next;
prev.next = curr.next;
curr.next = null;
}
}
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap obj = new MyHashMap();
* obj.put(key,value);
* int param_2 = obj.get(key);
* obj.remove(key);
*/