LeetCode 706: Design HashMap Solution
Master LeetCode problem 706 (Design HashMap), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
706. Design HashMap
Problem Explanation
Explanation:
To implement a HashMap, we can use an array of linked lists. The key is hashed to get the index in the array where the key-value pair should be stored. Each index in the array will store a linked list of key-value pairs. When inserting a new key-value pair, we first check if the key already exists in the linked list at the corresponding index. If it does, we update the value, otherwise, we add a new node to the linked list. When getting a value for a key, we hash the key to find the corresponding index and then search for the key in the linked list at that index. When removing a key, we find the key in the linked list and remove it.
- Time complexity for put, get, and remove operations: O(n) where n is the number of elements in the hashmap.
- Space complexity: O(n) considering the space used by the array and linked lists.
:
Solution Code
class MyHashMap {
private final int SIZE = 10000;
private List<Node>[] map;
class Node {
int key;
int value;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
/** Initialize your data structure here. */
public MyHashMap() {
map = new ArrayList[SIZE];
}
/** value will always be non-negative. */
public void put(int key, int value) {
int index = key % SIZE;
if (map[index] == null) {
map[index] = new ArrayList<>();
}
for (Node node : map[index]) {
if (node.key == key) {
node.value = value;
return;
}
}
map[index].add(new Node(key, 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 index = key % SIZE;
if (map[index] == null) {
return -1;
}
for (Node node : map[index]) {
if (node.key == key) {
return node.value;
}
}
return -1;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
public void remove(int key) {
int index = key % SIZE;
if (map[index] == null) {
return;
}
map[index].removeIf(node -> node.key == key);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 706 (Design HashMap)?
This page provides optimized solutions for LeetCode problem 706 (Design HashMap) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 706 (Design HashMap)?
The time complexity for LeetCode 706 (Design HashMap) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 706 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 706 in Java, C++, or Python.