0706-设计哈希映射

Raphael Liu Lv10

不使用任何内建的哈希表库设计一个哈希映射(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

示例:

**输入** :
["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]]

提示:

  • 0 <= key, value <= 106
  • 最多调用 104putgetremove 方法

方法一:链地址法

我们假定读者已经完成了「705. 设计哈希集合 」这一题目。

「设计哈希映射」与「设计哈希集合」解法接近,唯一的区别在于我们存储的不是 key 本身,而是 (\textit{key}, \textit{value}) 对。除此之外,代码基本是类似的。

[sol1-C++]
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
class MyHashMap {
private:
vector<list<pair<int, int>>> data;
static const int base = 769;
static int hash(int key) {
return key % base;
}
public:
/** Initialize your data structure here. */
MyHashMap(): data(base) {}

/** value will always be non-negative. */
void put(int key, int value) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it).first == key) {
(*it).second = value;
return;
}
}
data[h].push_back(make_pair(key, value));
}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it).first == key) {
return (*it).second;
}
}
return -1;
}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
int h = hash(key);
for (auto it = data[h].begin(); it != data[h].end(); it++) {
if ((*it).first == key) {
data[h].erase(it);
return;
}
}
}
};
[sol1-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
class MyHashMap {
private class Pair {
private int key;
private int value;

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

public int getKey() {
return key;
}

public int getValue() {
return value;
}

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

private static final int BASE = 769;
private LinkedList[] data;

/** 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);
Iterator<Pair> iterator = data[h].iterator();
while (iterator.hasNext()) {
Pair pair = iterator.next();
if (pair.getKey() == key) {
pair.setValue(value);
return;
}
}
data[h].offerLast(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> iterator = data[h].iterator();
while (iterator.hasNext()) {
Pair pair = iterator.next();
if (pair.getKey() == key) {
return pair.value;
}
}
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> iterator = data[h].iterator();
while (iterator.hasNext()) {
Pair pair = iterator.next();
if (pair.key == key) {
data[h].remove(pair);
return;
}
}
}

private static int hash(int key) {
return key % BASE;
}
}
[sol1-Golang]
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
const base = 769

type entry struct {
key, value int
}

type MyHashMap struct {
data []list.List
}

func Constructor() MyHashMap {
return MyHashMap{make([]list.List, base)}
}

func (m *MyHashMap) hash(key int) int {
return key % base
}

func (m *MyHashMap) Put(key, value int) {
h := m.hash(key)
for e := m.data[h].Front(); e != nil; e = e.Next() {
if et := e.Value.(entry); et.key == key {
e.Value = entry{key, value}
return
}
}
m.data[h].PushBack(entry{key, value})
}

func (m *MyHashMap) Get(key int) int {
h := m.hash(key)
for e := m.data[h].Front(); e != nil; e = e.Next() {
if et := e.Value.(entry); et.key == key {
return et.value
}
}
return -1
}

func (m *MyHashMap) Remove(key int) {
h := m.hash(key)
for e := m.data[h].Front(); e != nil; e = e.Next() {
if e.Value.(entry).key == key {
m.data[h].Remove(e)
}
}
}
[sol1-JavaScript]
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
var MyHashMap = function() {
this.BASE = 769;
this.data = new Array(this.BASE).fill(0).map(() => new Array());
};

MyHashMap.prototype.put = function(key, value) {
const h = this.hash(key);
for (const it of this.data[h]) {
if (it[0] === key) {
it[1] = value;
return;
}
}
this.data[h].push([key, value]);
};

MyHashMap.prototype.get = function(key) {
const h = this.hash(key);
for (const it of this.data[h]) {
if (it[0] === key) {
return it[1];
}
}
return -1;
};

MyHashMap.prototype.remove = function(key) {
const h = this.hash(key);
for (const it of this.data[h]) {
if (it[0] === key) {
const idx = this.data[h].indexOf(it);
this.data[h].splice(idx, 1);
return;
}
}
};

MyHashMap.prototype.hash = function(key) {
return key % this.BASE;
}
[sol1-C]
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
struct List {
int key;
int val;
struct List* next;
};

void listPush(struct List* head, int key, int val) {
struct List* tmp = malloc(sizeof(struct List));
tmp->key = key;
tmp->val = val;
tmp->next = head->next;
head->next = tmp;
}

void listDelete(struct List* head, int key) {
for (struct List* it = head; it->next; it = it->next) {
if (it->next->key == key) {
struct List* tmp = it->next;
it->next = tmp->next;
free(tmp);
break;
}
}
}

struct List* listFind(struct List* head, int key) {
for (struct List* it = head; it->next; it = it->next) {
if (it->next->key == key) {
return it->next;
}
}
return NULL;
}

void listFree(struct List* head) {
while (head->next) {
struct List* tmp = head->next;
head->next = tmp->next;
free(tmp);
}
}

const int base = 769;

int hash(int key) {
return key % base;
}

typedef struct {
struct List* data;
} MyHashMap;

MyHashMap* myHashMapCreate() {
MyHashMap* ret = malloc(sizeof(MyHashMap));
ret->data = malloc(sizeof(struct List) * base);
for (int i = 0; i < base; i++) {
ret->data[i].key = 0;
ret->data[i].val = 0;
ret->data[i].next = NULL;
}
return ret;
}

void myHashMapPut(MyHashMap* obj, int key, int value) {
int h = hash(key);
struct List* rec = listFind(&(obj->data[h]), key);
if (rec == NULL) {
listPush(&(obj->data[h]), key, value);
} else {
rec->val = value;
}
}

int myHashMapGet(MyHashMap* obj, int key) {
int h = hash(key);
struct List* rec = listFind(&(obj->data[h]), key);
if (rec == NULL) {
return -1;
} else {
return rec->val;
}
}

void myHashMapRemove(MyHashMap* obj, int key) {
int h = hash(key);
listDelete(&(obj->data[h]), key);
}

void myHashMapFree(MyHashMap* obj) {
for (int i = 0; i < base; i++) {
listFree(&(obj->data[i]));
}
free(obj->data);
}

复杂度分析

  • 时间复杂度:O(n}{b})。其中 n 为哈希表中的元素数量,b 为链表的数量。假设哈希值是均匀分布的,则每个链表大概长度为 n}{b。

  • 空间复杂度:O(n+b)。

 Comments
On this page
0706-设计哈希映射