[leetcode]706. Design HashMap设计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.

code

 1 class MyHashMap {
 2     /** Initialize your data structure here. */
 3     int size = 1000;
 4     ListNode[] bucket = new ListNode[size];
 5     
 6     public MyHashMap() {}
 7     
 8     /** value will always be non-negative. */
 9     public void put(int key, int value) {
10          int i = idx(key);
11         
12          if(bucket[i] == null) {
13              bucket[i] = new ListNode(-1, -1);
14          } 
15          ListNode pre = null;
16          ListNode node = bucket[i];
17 
18          while(node != null && node.key != key) {
19              pre = node;
20              node = node.next;
21          }
22         
23          if(node == null) {
24              pre.next = new ListNode(key, value);
25          } else {
26              node.val = value;
27          }
28     }
29     
30     /** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
31     public int get(int key) {
32         ListNode node = bucket[idx(key)];
33         while(node != null && node.key != key) {
34              node = node.next;
35         }
36         return node == null ? -1 : node.val;
37     }
38     
39     /** Removes the mapping of the specified value key if this map contains a mapping for the key */
40     public void remove(int key) {
41         ListNode node = bucket[idx(key)];
42         ListNode pre = null;
43         while(node != null && node.key != key) {
44              pre = node;
45              node = node.next;
46         }
47         if(node != null && node.key == key) {
48             pre.next = node.next;
49         }
50     }   
51     
52     public int idx(int key) {    
53         return Integer.hashCode(key) % size;
54     }
55 }
56 
57 
58 class ListNode{
59     int key;
60     int val;
61     ListNode next;
62     public ListNode(int k, int v){
63         this.key = k;
64         this.val = v;
65     }
66 }
67 
68 /**
69  * Your MyHashMap object will be instantiated and called as such:
70  * MyHashMap obj = new MyHashMap();
71  * obj.put(key,value);
72  * int param_2 = obj.get(key);
73  * obj.remove(key);
74  */

猜你喜欢

转载自www.cnblogs.com/liuliu5151/p/10873335.html
今日推荐