力扣 706. 设计哈希映射

题目说明

不使用任何内建的哈希表库设计一个哈希映射(HashMap)。

实现 MyHashMap 类:

  • MyHashMap() 用空映射初始化对象
  • void put(int key, int value) 向 HashMap 插入一个键值对 (key, value) 。如果 key 已经存在于映射中,则更新其对应的值 value 。
  • int get(int key) 返回特定的 key 所映射的 value ;如果映射中不包含 key 的映射,返回 -1 。
  • void remove(key) 如果映射中存在 key 的映射,则移除 key 和它所对应的 value 。
  • 提示:
    • 0 <= key, value <= 10^6
    • 最多调用 10^4putgetremove 方法

示例

示例 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
输入:
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
输出:
[null, null, null, 1, -1, null, 1, null, -1]

解释:
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // myHashMap 现在为 [[1,1]]
myHashMap.put(2, 2); // myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(1); // 返回 1 ,myHashMap 现在为 [[1,1], [2,2]]
myHashMap.get(3); // 返回 -1(未找到),myHashMap 现在为 [[1,1], [2,2]]
myHashMap.put(2, 1); // myHashMap 现在为 [[1,1], [2,1]](更新已有的值)
myHashMap.get(2); // 返回 1 ,myHashMap 现在为 [[1,1], [2,1]]
myHashMap.remove(2); // 删除键为 2 的数据,myHashMap 现在为 [[1,1]]
myHashMap.get(2); // 返回 -1(未找到),myHashMap 现在为 [[1,1]]

笔者理解

此题是一道数据结构设计算法问题,在力扣题库中被定义为简单题。

解法

当笔者阅读完此题后,直接采用了链表数组的方式,让我们来看看具体如何实现的吧。

实现

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
class MyHashMap {

/**
* 键值对内部类
*/
class Pair {
private int key;
private int value;

public Pair(int key, int value) {
this.key = key;
this.value = value;
}

public int getKay() {
return key;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}
}

// 预定内存大小
private static final int BASE = 769;

// 链表数组, jdk 1.8 之前 HashMap 的主要实现方式
private LinkedList[] data;

// 计算哈希值
private static int hash(int num) {
return num % BASE;
}

/** Initialize your data structure here. */
public MyHashMap() {
data = new LinkedList[BASE];

// 初始化各个链表
for (int i = 0; i < BASE; i++) {
data[i] = new LinkedList<Pair>();
}
}

/** value will always be non-negative. */
public void put(int key, int value) {
int h = hash(key);
// 获取迭代器, AbstractSequentialList 定义的方法 iterator()
Iterator<Pair> iter = data[h].iterator();
while (iter.hasNext()) {
Pair pair = iter.next();
// 找到对应键值就改变
if (pair.getKay() == key) {
pair.setValue(value);
return ;
}
}
// 没找到就添加
data[h].addLast(new Pair(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 h = hash(key);
Iterator<Pair> iter = data[h].iterator();
while (iter.hasNext()) {
Pair pair = iter.next();
if (pair.getKay() == key) {
return pair.getValue();
}
}
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 h = hash(key);
Iterator<Pair> iter = data[h].iterator();
while (iter.hasNext()) {
Pair pair = iter.next();
if (pair.getKay() == key) {
data[h].remove(pair);
return ;
}
}
}
}

/**
* 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);
*/

时间和空间效率还行,可见此解法还比较适合此题。

image-20210916105128403

总结

本题是今天的一题,难度是为简单,感兴趣的朋友都可以去尝试一下,此题还有其他更多的解法,朋友们可以自己逐一尝试。