-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0706.java
More file actions
72 lines (63 loc) · 1.95 KB
/
_0706.java
File metadata and controls
72 lines (63 loc) · 1.95 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
package com.github.aditya;
import java.util.LinkedList;
public class _0706 {
// 16 ms, faster than 92.13%, memory 48.6 MB, less than 89.92%
class MyHashMap {
public static int SIZE = 809;
LinkedList<Entry>[] map;
public MyHashMap() {
map = new LinkedList[SIZE];
}
public void put(int key, int value) {
int index = key % SIZE;
if (map[index] == null) {
map[index] = new LinkedList<>();
map[index].add(new Entry(key, value));
} else {
for (Entry entry : map[index]) {
if (entry.key == key) {
entry.value = value;
return;
}
}
map[index].add(new Entry(key, value));
}
}
public int get(int key) {
int index = key % SIZE;
LinkedList<Entry> childList = map[index];
if (childList == null) return -1;
for (Entry entry : childList) {
if (entry.key == key)
return entry.value;
}
return -1;
}
public void remove(int key) {
int index = key % SIZE;
if (map[index] == null) return;
Entry removeObj = null;
for (Entry entry : map[index]) {
if (entry.key == key)
removeObj = entry;
}
if (removeObj == null) return;
map[index].remove(removeObj);
}
private class Entry {
public int key;
public int value;
public Entry(int key, int value) {
this.key = key;
this.value = value;
}
}
}
/**
* 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);
*/
}