|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/**706. Design HashMap |
| 4 | + * |
| 5 | + * Design a HashMap without using any built-in hash table libraries. |
| 6 | + * |
| 7 | + * To be specific, your design should include these functions: |
| 8 | + * |
| 9 | + * put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value. |
| 10 | + * get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. |
| 11 | + * remove(key) : Remove the mapping for the value key if this map contains the mapping for the key. |
| 12 | + * |
| 13 | + * Example: |
| 14 | + * |
| 15 | + * MyHashMap hashMap = new MyHashMap(); |
| 16 | + * hashMap.put(1, 1); |
| 17 | + * hashMap.put(2, 2); |
| 18 | + * hashMap.get(1); // returns 1 |
| 19 | + * hashMap.get(3); // returns -1 (not found) |
| 20 | + * hashMap.put(2, 1); // update the existing value |
| 21 | + * hashMap.get(2); // returns 1 |
| 22 | + * hashMap.remove(2); // remove the mapping for 2 |
| 23 | + * hashMap.get(2); // returns -1 (not found) |
| 24 | + * |
| 25 | + * Note: |
| 26 | + * |
| 27 | + * All keys and values will be in the range of [0, 1000000]. |
| 28 | + * The number of operations will be in the range of [1, 10000]. |
| 29 | + * Please do not use the built-in HashMap library.*/ |
| 30 | +public class _706 { |
| 31 | + public static class Solution1 { |
| 32 | + /** |
| 33 | + * credit: https://leetcode.com/problems/design-hashmap/discuss/152746/Java-Solution |
| 34 | + */ |
| 35 | + class MyHashMap { |
| 36 | + |
| 37 | + final ListNode[] nodes = new ListNode[10000]; |
| 38 | + |
| 39 | + public void put(int key, int value) { |
| 40 | + int i = idx(key); |
| 41 | + if (nodes[i] == null) { |
| 42 | + nodes[i] = new ListNode(-1, -1); |
| 43 | + } |
| 44 | + ListNode prev = find(nodes[i], key); |
| 45 | + if (prev.next == null) { |
| 46 | + prev.next = new ListNode(key, value); |
| 47 | + } else { |
| 48 | + prev.next.val = value; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public int get(int key) { |
| 53 | + int i = idx(key); |
| 54 | + if (nodes[i] == null) { |
| 55 | + return -1; |
| 56 | + } |
| 57 | + ListNode node = find(nodes[i], key); |
| 58 | + return node.next == null ? -1 : node.next.val; |
| 59 | + } |
| 60 | + |
| 61 | + public void remove(int key) { |
| 62 | + int i = idx(key); |
| 63 | + if (nodes[i] == null) { |
| 64 | + return; |
| 65 | + } |
| 66 | + ListNode prev = find(nodes[i], key); |
| 67 | + if (prev.next == null) { |
| 68 | + return; |
| 69 | + } |
| 70 | + prev.next = prev.next.next; |
| 71 | + } |
| 72 | + |
| 73 | + int idx(int key) { |
| 74 | + return Integer.hashCode(key) % nodes.length; |
| 75 | + } |
| 76 | + |
| 77 | + ListNode find(ListNode bucket, int key) { |
| 78 | + ListNode node = bucket, prev = null; |
| 79 | + while (node != null && node.key != key) { |
| 80 | + prev = node; |
| 81 | + node = node.next; |
| 82 | + } |
| 83 | + return prev; |
| 84 | + } |
| 85 | + |
| 86 | + class ListNode { |
| 87 | + int key, val; |
| 88 | + ListNode next; |
| 89 | + |
| 90 | + ListNode(int key, int val) { |
| 91 | + this.key = key; |
| 92 | + this.val = val; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +/** |
| 98 | + * Your MyHashMap object will be instantiated and called as such: |
| 99 | + * MyHashMap obj = new MyHashMap(); |
| 100 | + * obj.put(key,value); |
| 101 | + * int param_2 = obj.get(key); |
| 102 | + * obj.remove(key); |
| 103 | + */ |
| 104 | + } |
| 105 | +} |
0 commit comments