forked from super30admin/Design-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHashMap.java
113 lines (101 loc) · 2.15 KB
/
MyHashMap.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Approach - Using a custom node class to store key, val and next
Created a new Node[] to store all the primary keys which would point to the head node
put - TC - O(1) SC - O(1)
get - TC - O(1) SC - O(1)
remove - TC - O(1) and SC - O(1)
*/
/**
* MyHashMap : MyHashMap
*
* @author : Kavya Mandaliya ([email protected])
* @version : 1.0 (Oct 23, 2024)
* @since : 1.0 (Oct 23, 2024)
*/
class MyHashMap {
class Node{
int key;
int val;
Node next;
public Node(int key, int val){
this.key = key;
this.val = val;
this.next = null;
}
}
Node[] map;
public MyHashMap() {
this.map = new Node[10000];
}
private int getPrimaryKey(int key){
return key % 10000;
}
private Node getPrev(Node head, int key){
Node prev = null;
while(head != null){
if(head.key == key){
break;
}else{
prev = head;
head = head.next;
}
}
return prev;
}
public void put(int key, int value) {
int primary = getPrimaryKey(key);
if(map[primary] == null){
map[primary] = new Node(-1, -1);
}
Node head = map[primary];
Node prev = getPrev(head, key);
Node curr = new Node(key, value);
if(prev.next == null){
prev.next = curr;
}else{
prev.next.val = value;
}
}
public int get(int key) {
int primary = getPrimaryKey(key);
if(map[primary] == null){
return -1;
}
Node head = map[primary];
Node prev = getPrev(head, key);
if(prev.next == null){
return -1;
}
return prev.next.val;
}
public void remove(int key) {
int primary = getPrimaryKey(key);
if(map[primary] == null){
return;
}
Node head = map[primary];
Node prev = getPrev(head, key);
if(prev == null){
return;
}
Node curr = prev.next;
prev.next = curr.next;
curr.next = null;
}
public static void main(String[] args) {
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1);
myHashMap.put(2, 2);
myHashMap.put(3, 3);
System.out.println(myHashMap.get(2));
myHashMap.remove(2);
System.out.println(myHashMap.get(2));
}
}
/**
* 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);
*/