[LeetCode] 706. Design HashMap

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

  • MyHashMap() initializes the object with an empty map.
  • void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
  • int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
  • void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.

Example 1:

Input
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output
[null, null, null, 1, -1, null, 1, null, -1]

Explanation
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1);    // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3);    // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2);    // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2);    // return -1 (i.e., not found), The map is now [[1,1]]

Constraints:

  • 0 <= key, value <= 106
  • At most 104 calls will be made to putget, and remove.

设计哈希映射。

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

题意很简单,不过这道题实现起来,代码量稍微有点多的。我这里给出两种方案。

首先是直接用数组。因为条件里面说了 key 和 value 的范围是正数,所以不存在负数的情况,所以可以用数组来处理。再加上数据的范围不是那么大,所以这道题用数组是可以解决的。

 1 class MyHashMap {
 2     int[] table;
 3 
 4     /** Initialize your data structure here. */
 5     public MyHashMap() {
 6         table = new int[1000001];
 7         Arrays.fill(table, -1);
 8     }
 9 
10     /** value will always be non-negative. */
11     public void put(int key, int value) {
12         table[key] = value;
13     }
14 
15     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
16     public int get(int key) {
17         return table[key];
18     }
19 
20     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
21     public void remove(int key) {
22         table[key] = -1;
23     }
24 }
25 
26 /**
27  * Your MyHashMap object will be instantiated and called as such:
28  * MyHashMap obj = new MyHashMap();
29  * obj.put(key,value);
30  * int param_2 = obj.get(key);
31  * obj.remove(key);
32  */

 

这里我再提供一个 list 的做法。需要创建两个 list of list,一个用来记录 key,一个用来记录 value,这样 key 和 value 的键值对才能对应起来。这两个 list of list 的长度我给了 500,不是很大,但是解决这个题是够了。当看到一个 key 的时候,为了避免哈希碰撞,我把这个 key % 500。因为我创建的是 list of list,所以即使再有冲突也不怕,加到那个对应的 sublist 里面即可。value 加入的位置始终跟 key 被加入的位置一样。

 1 class MyHashMap {
 2     List<List<Integer>> keys;
 3     List<List<Integer>> values;
 4     int len = 500;
 5 
 6     /** Initialize your data structure here. */
 7     public MyHashMap() {
 8         keys = new ArrayList<>();
 9         values = new ArrayList<>();
10         for (int i = 0; i < len; i++) {
11             keys.add(i, new ArrayList<>());
12             values.add(i, new ArrayList<>());
13         }
14     }
15 
16     private boolean contains(int key) {
17         return keys.get(key % len).contains(key);
18     }
19     
20     /** value will always be non-negative. */
21     public void put(int key, int value) {
22         if (contains(key)) {
23             remove(key);
24         }
25         keys.get(key % len).add(key);
26         values.get(key % len).add(value);
27     }
28     
29     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
30     public int get(int key) {
31         if (contains(key)) {
32             int index = keys.get(key % len).indexOf((Integer) key);
33             return values.get(key % len).get(index);
34         }
35         return -1;
36     }
37     
38     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
39     public void remove(int key) {
40         if (contains(key)) {
41             int index = keys.get(key % len).indexOf((Integer) key);
42             keys.get(key % len).remove(index);
43             values.get(key % len).remove(index);
44         }
45     }
46 }
47 
48 /**
49  * Your MyHashMap object will be instantiated and called as such:
50  * MyHashMap obj = new MyHashMap();
51  * obj.put(key,value);
52  * int param_2 = obj.get(key);
53  * obj.remove(key);
54  */

 

LeetCode 题目总结

posted @ 2020-08-27 07:11  CNoodle  阅读(195)  评论(0)    收藏  举报