-
Notifications
You must be signed in to change notification settings - Fork 0
/
design_HashMap_lc706.java
87 lines (73 loc) · 1.99 KB
/
design_HashMap_lc706.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
class Pair<K,V> {
public K key;
public V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
}
class Bucket {
private List<Pair<Integer, Integer>> bucket;
public Bucket() {
this.bucket = new LinkedList<>();
}
public Integer get(Integer key) {
for (var pair: bucket) {
if (pair.key.equals(key)) {
return pair.value;
}
}
return -1;
}
public void remove(Integer key) {
for (var pair: bucket) {
if (pair.key.equals(key)) {
bucket.remove(pair);
return;
}
}
}
public void update(Integer key, Integer value) {
boolean found = false;
for (var pair: bucket) {
if (pair.key.equals(key)) {
pair.value = value;
found = true;
break;
}
}
if(found!=true) {
bucket.add(new Pair<Integer, Integer>(key, value));
}
}
}
class MyHashMap {
private final int KEY_SPAN=2345;
private List<Bucket> hashTable;
public MyHashMap() {
this.hashTable = new ArrayList<Bucket>();
for (int i =0; i< KEY_SPAN; i++) {
this.hashTable.add(new Bucket());
}
}
public void put(int key, int value) {
this.hashTable.get(key%KEY_SPAN).update(key, value);
}
public int get(int key) {
return this.hashTable.get(key%KEY_SPAN).get(key);
}
public void remove(int key) {
this.hashTable.get(key%KEY_SPAN).remove(key);
}
}
/**
* 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);
*/
/*
Runtime: 41 ms, faster than 45.87% of Java online submissions for Design HashMap.
Memory Usage: 66.8 MB, less than 42.94% of Java online submissions for Design HashMap.
*/