-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_706.java
195 lines (170 loc) · 5.88 KB
/
_706.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.fishercoder.solutions.firstthousand;
import java.util.Arrays;
public class _706 {
public static class Solution1 {
/*
* credit: https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution
*/
class MyHashMap {
final ListNode[] nodes = new ListNode[10000];
public void put(int key, int value) {
int i = idx(key);
if (nodes[i] == null) {
nodes[i] = new ListNode(-1, -1);
}
ListNode prev = find(nodes[i], key);
if (prev.next == null) {
prev.next = new ListNode(key, value);
} else {
prev.next.val = value;
}
}
public int get(int key) {
int i = idx(key);
if (nodes[i] == null) {
return -1;
}
ListNode node = find(nodes[i], key);
return node.next == null ? -1 : node.next.val;
}
public void remove(int key) {
int i = idx(key);
if (nodes[i] == null) {
return;
}
ListNode prev = find(nodes[i], key);
if (prev.next == null) {
return;
}
prev.next = prev.next.next;
}
int idx(int key) {
return Integer.hashCode(key) % nodes.length;
}
ListNode find(ListNode bucket, int key) {
ListNode node = bucket;
ListNode prev = null;
while (node != null && node.key != key) {
prev = node;
node = node.next;
}
return prev;
}
class ListNode {
int key;
int val;
ListNode next;
ListNode(int key, int val) {
this.key = key;
this.val = val;
}
}
}
}
public static class Solution2 {
public static class MyHashMap {
/*
* Considering the given constraints for this problem on LeetCode, load factors and resizing/rehashing are not considered. Thus an EASY problem.
* <p>
* inspired by: https://leetcode.com/problems/design-hashmap/discuss/225312/hashmaparraylinkedlistcollision
*/
class Node {
/*
* We need to have both key and val in this ListNode because all values input key are hashed to the same bucket, so we need its original key
* to be a differentiator within this bucket.
*/
int val;
int key;
Node next;
public Node(int key, int val) {
this.key = key;
this.val = val;
}
}
Node[] nodes;
int size = 1000000;
/*
* Initialize your data structure here.
*/
public MyHashMap() {
nodes = new Node[size];
}
/*
* value will always be non-negative.
*/
public void put(int key, int value) {
int index = getHashedKey(key);
Node head = nodes[index];
Node tmp = head;
while (tmp != null) {
if (tmp.key == key) {
tmp.val = value;
return;
}
tmp = tmp.next;
}
Node newHead = new Node(key, value);
newHead.next = head;
nodes[index] = newHead;
}
private int getHashedKey(int key) {
return Integer.hashCode(key) % size;
}
/*
* Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
*/
public int get(int key) {
Node head = nodes[getHashedKey(key)];
Node tmp = head;
while (tmp != null && tmp.key != key) {
tmp = tmp.next;
}
if (tmp == null) {
return -1;
}
if (tmp.key == key) {
return tmp.val;
}
return -1;
}
/*
* Removes the mapping of the specified value key if this map contains a mapping for the key
*/
public void remove(int key) {
/*We can just set the value of this key to -1 since the constraint for this problem is that all values are >= 0*/
Node node = nodes[getHashedKey(key)];
Node tmp = node;
Node pre = new Node(-1, -1);
pre.next = tmp;
while (tmp != null) {
if (tmp.key == key) {
tmp.val = -1;
return;
}
tmp = tmp.next;
}
}
}
}
public static class Solution3 {
/*
* My completely original, but hacky and cheaty solution to take full advantage of the problem constraints.
*/
public static class MyHashMap {
int[] map;
public MyHashMap() {
map = new int[1000001];
Arrays.fill(map, -1);
}
public void put(int key, int value) {
map[key] = value;
}
public int get(int key) {
return map[key];
}
public void remove(int key) {
map[key] = -1;
}
}
}
}