LeetCode 706. Design HashMap

原题链接在这里:https://leetcode.com/problems/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.

题解:

Like HashMap, there is an array of link list node.

Get the hash and find the position in array and go through each node in that list to check the key.

Time Complexity: put, O(1). find, O(1). remove, O(1).

Space: O(size). size of array.

AC Java:

 1 class MyHashMap {
 2     int size = 100000;
 3     Node [] arr;
 4     
 5     /** Initialize your data structure here. */
 6     public MyHashMap() {
 7         arr = new Node[size];
 8         for(int i = 0; i < arr.length; i++){
 9             arr[i] = new Node(-1, -1);
10         }
11     }
12     
13     /** value will always be non-negative. */
14     public void put(int key, int value) {
15         Node p = findNode(key);
16         
17         if(p.next == null){
18             p.next = new Node(key, value);
19         }else{
20             p.next.val = value;
21         }
22     }
23     
24     private Node findNode(int key){
25         int hash = getHash(key);
26         Node p = arr[hash];
27         
28         while(p.next != null && p.next.key != key){
29             p = p.next;
30         }
31         
32         return p;
33     }
34     
35     private int getHash(int n){
36         return n % size;
37     }
38     
39     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
40     public int get(int key) {
41         Node p = findNode(key);
42         
43         if(p.next == null){
44             return -1;
45         }
46         
47         return p.next.val;
48     }
49     
50     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
51     public void remove(int key) {
52         Node p = findNode(key);
53         
54         if(p.next == null){
55             return;
56         }
57         
58         p.next = p.next.next;
59     }
60 }
61 
62 class Node{
63     int key;
64     int val;
65     Node next;
66     public Node(int key, int val){
67         this.key = key;
68         this.val = val;
69     }
70 }
71 
72 /**
73  * Your MyHashMap object will be instantiated and called as such:
74  * MyHashMap obj = new MyHashMap();
75  * obj.put(key,value);
76  * int param_2 = obj.get(key);
77  * obj.remove(key);
78  */
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12196080.html