-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLc706.java
More file actions
56 lines (51 loc) · 1.4 KB
/
Lc706.java
File metadata and controls
56 lines (51 loc) · 1.4 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
package leetcode;
import java.util.ArrayList;
/**
* @author Kuma
* @date 2021年3月14日
* 706. 设计哈希映射
*/
public class Lc706 {
}
class MyHashMap {
ArrayList<ArrayList<int[]>> hashmap;
int max = 50;
/** Initialize your data structure here. */
public MyHashMap() {
hashmap = new ArrayList<>();
for (int i = 0; i < max; i++) {
hashmap.add(new ArrayList<>());
}
}
/** value will always be non-negative. */
public void put(int key, int value) {
int code = key % max;
for (int[] i : hashmap.get(code)){
if (i[0] == key){
i[1] = value;
return;
}
}
hashmap.get(code).add(new int[]{key,value});
}
/** 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) {
int code = key % max;
for (int[] i : hashmap.get(code)){
if (i[0] == key){
return i[1];
}
}
return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int code = key % max;
for (int[] i : hashmap.get(code)){
if (i[0] == key){
hashmap.get(code).remove(i);
break;
}
}
}
}