• [LeetCode] 706. Design HashMap


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

    To be specific, your design should include these functions:

    • put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
    • get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
    • remove(key) : Remove the mapping for the value key if this map contains the mapping for the key.

    Example:

    MyHashMap hashMap = new MyHashMap();
    hashMap.put(1, 1);          
    hashMap.put(2, 2);         
    hashMap.get(1);            // returns 1
    hashMap.get(3);            // returns -1 (not found)
    hashMap.put(2, 1);          // update the existing value
    hashMap.get(2);            // returns 1 
    hashMap.remove(2);          // remove the mapping for 2
    hashMap.get(2);            // returns -1 (not found)

    Note:

      • All keys and values will be in the range of [0, 1000000].
      • The number of operations will be in the range of [1, 10000].
      • Please do not use the built-in HashMap library.

    设计哈希映射。

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

    首先是直接用数组。因为条件里面说了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 题目总结

  • 相关阅读:
    Python之struct模块浅谈
    看头发知健康
    ZeroMQ:云计算时代最好的通讯库
    粗盐热敷疗法经验汇总
    百度2011校招笔试算法题一
    new/delete 和malloc/free 的区别一般汇总
    Trie字典树
    百度2012校招笔试题之全排列与组合
    百度2011校招笔试算法题二
    执行程序的内存分布总结
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13569253.html
Copyright © 2020-2023  润新知