706. Design HashMap 实现哈希表

[抄题]:

 

public MyHashMap() {
 主函数里面是装非final变量的,如果没有,可以一个字都不写
}

 

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

put: 不论如何都要放。所以如果没有bucket,就要new一个对象出来:

 

if (buckets[i] == null)
                buckets[i] = new Bucket();

 

get:没有就返回-1

remove:没有就直接return

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

Bucket类都用final变量,表示容量恒定不变

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

找上一个的bucket[index] ,根据上一个来找下一个:

 

 

[一刷]:

  1. ListNode类中包括ListNode方法
  2. find函数也是返回前一个prev节点

[二刷]:

  1. find函数查找的是key,不是value

[三刷]:

  1. 后面三个函数都要调用find函数啊,不然find干嘛的
  2. 都要考虑prev.next不存在的情况

[四刷]:

  1.  bucket是一个链表,find(Bucket bucket, int key)要在该链表中查找,而不是数组中

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

Bucket是一个链表,根据上一个节点来找下一个

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 [是否头一次写此类driver funcion的代码] :

class MyHashMap {

    /** Initialize your data structure here. */
    public MyHashMap() {
        
    }
    
    //ini: class ListNode, class bucket
    class ListNode {
        int key, val;
        ListNode next;
        
        ListNode(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    
    class Bucket {
        final ListNode head = new ListNode(-1, -1);
    }
    
    final Bucket[] buckets = new Bucket[10000000];

    //method find, shuld function just as the normal find
    ListNode find(Bucket buckets, int key) {
        ListNode prev = null; ListNode cur = buckets.head;
        //while loop
        while (cur != null && cur.key != key) {
            prev = cur;
            cur = cur.next;
        }
        
        return prev;
    }
    
    //method getIdx
    int getIdx(int key) {
        return Integer.hashCode(key) % buckets.length;
    }
    
    /** value will always be non-negative. */
    public void put(int key, int value) {
        int i = getIdx(key);
        //cc 
        if (buckets[i] == null) 
            buckets[i] = new Bucket();
        ListNode prev = find(buckets[i], key);
        //the bucket's next is null or not
        if (prev.next == null) {
            prev.next = new ListNode(key, value);
        }else {
            prev.next.val = 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 i = getIdx(key);
        //cc 
        if (buckets[i] == null) return -1;
        ListNode prev = find(buckets[i], key);
        return prev.next == null ? -1 : prev.next.val;
    }
    
    /** Removes the mapping of the specified value key if this map contains a mapping for the key */
    public void remove(int key) {
        int i = getIdx(key);
        //cc 
        if (buckets[i] == null) return ;
        ListNode prev = find(buckets[i], key);
        if (prev.next == null) return;
        prev.next = prev.next.next;
    }
}

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

 [潜台词] :

 

posted @ 2018-07-29 18:12  苗妙苗  阅读(547)  评论(0编辑  收藏  举报