-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathMyHashMap.java
More file actions
88 lines (82 loc) · 2.25 KB
/
MyHashMap.java
File metadata and controls
88 lines (82 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
88
/*
I use an array of buckets where each bucket stores a linked list to handle hash collisions using separate chaining.
The key is mapped to a bucket index using a hash function (key % size), and all operations traverse only that bucket.
This gives average O(1) time for add, remove, and contains, with O(n) worst case if many keys collide.
Time Complexity (average):
add: O(1)
remove: O(1)
contains: O(1)
Worst case (many collisions in one bucket):
O(n)
Space Complexity:
O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
*/
class MyHashMap {
private class Node{
int key;
int value;
Node next;
public Node(int key, int value){
this.key =key;
this.value = value;
}
}
private static final int DEFAULT_CAPACITY = 1000;
private Node [] buckets;
public MyHashMap() {
buckets = new Node[DEFAULT_CAPACITY];
}
private int hash(int key){
return key % DEFAULT_CAPACITY;
}
public void put(int key, int value) {
int hash = hash(key);
Node cur = buckets[hash];
if(buckets[hash] == null){
buckets[hash] = new Node(key, value);
return;
}
while(true){
if(cur.key == key){
cur.value = value;
return;
}
if(cur.next == null) break;
cur = cur.next;
}
cur.next = new Node(key, value);
}
public int get(int key) {
int hash = hash(key);
Node cur = buckets[hash];
if(buckets[hash] == null){
return -1;
}
while(cur != null){
if(cur.key == key){
return cur.value;
}
cur = cur.next;
}
return -1;
}
public void remove(int key) {
int hash = hash(key);
Node cur = buckets[hash];
if (cur == null) return;
// remove head
if(cur.key == key){
buckets[hash] = cur.next;
return;
}
while(cur.next != null){
if(cur.next.key == key){
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
}