Skip to content

Commit 4344ba9

Browse files
solves design hash map
1 parent 4133031 commit 4344ba9

File tree

3 files changed

+137
-4
lines changed

3 files changed

+137
-4
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-173/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-173/2081-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-173/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-174/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-174/2081-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-174/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
88

@@ -194,7 +194,7 @@
194194
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | [![Java](assets/java.png)](src/KthLargestElementInAStream.java) [![Python](assets/python.png)](python/k_th_largest_element_in_a_stream.py) |
195195
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | [![Java](assets/java.png)](src/BinarySearch.java) [![Python](assets/python.png)](python/binary_search.py) |
196196
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset) | [![Java](assets/java.png)](src/DesignHashSet.java) [![Python](assets/python.png)](python/design_hash_set.py) |
197-
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap) | |
197+
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap) | [![Java](assets/java.png)](src/DesignHashMap.java) [![Python](assets/python.png)](python/design_hash_map.py) |
198198
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case) | |
199199
| 716 | [Max Stack](https://leetcode.com/problems/max-stack) | |
200200
| 717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters) | |

Diff for: python/design_hash_map.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Bucket:
2+
def __init__(self):
3+
self.list = []
4+
5+
def __str__(self):
6+
return self.list.__str__()
7+
8+
def contains(self, key: int) -> bool:
9+
for listKey, value in self.list:
10+
if key == listKey:
11+
return True
12+
return False
13+
14+
def put(self, key: int, value: int) -> None:
15+
changed = False
16+
for index, (listKey, _) in enumerate(self.list):
17+
if listKey == key:
18+
self.list[index] = (key, value)
19+
changed = True
20+
break
21+
if not changed:
22+
self.list.append((key, value))
23+
24+
def remove(self, key: int) -> None:
25+
if self.contains(key):
26+
self.list.remove((key, self.get(key)))
27+
28+
def get(self, key: int) -> int:
29+
if self.contains(key):
30+
for listKey, value in self.list:
31+
if listKey == key:
32+
return value
33+
return -1
34+
35+
36+
class MyHashMap:
37+
38+
def __init__(self):
39+
self.initial_size = 100
40+
self.buckets = [Bucket() for _ in range(self.initial_size)]
41+
42+
def _hashCode(self, number: int) -> int:
43+
return number % len(self.buckets)
44+
45+
def put(self, key: int, value: int) -> None:
46+
self.buckets[self._hashCode(key)].put(key, value)
47+
48+
def get(self, key: int) -> int:
49+
return self.buckets[self._hashCode(key)].get(key)
50+
51+
def remove(self, key: int) -> None:
52+
self.buckets[self._hashCode(key)].remove(key)

Diff for: src/DesignHashMap.java

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import java.util.LinkedList;
2+
import java.util.Objects;
3+
import java.util.Queue;
4+
5+
public class DesignHashMap {
6+
class MyHashMap {
7+
8+
private final int initialSize = 100;
9+
private final Bucket[] buckets = new Bucket[initialSize];
10+
11+
public MyHashMap() {
12+
for (int index = 0 ; index < buckets.length ; index++) {
13+
buckets[index] = new Bucket();
14+
}
15+
}
16+
17+
public void put(int key, int value) {
18+
int hashCode = key % buckets.length;
19+
buckets[hashCode].put(key, value);
20+
}
21+
22+
public int get(int key) {
23+
return buckets[key % buckets.length].get(key);
24+
}
25+
26+
public void remove(int key) {
27+
buckets[key % buckets.length].remove(key);
28+
}
29+
30+
private class Bucket {
31+
Queue<Pair> list = new LinkedList<>();
32+
33+
34+
public boolean contains(int element) {
35+
return list.stream().anyMatch(pair -> pair.key == element);
36+
}
37+
38+
public void put(int key, int value) {
39+
if (contains(key)) {
40+
Pair match = list.stream().filter(pair -> pair.key == key).findFirst().get();
41+
match.value = value;
42+
} else {
43+
list.add(new Pair(key, value));
44+
}
45+
}
46+
47+
public int get(int key) {
48+
return contains(key) ? list.stream().filter(pair -> pair.key == key).findFirst().get().value : -1;
49+
}
50+
51+
public void remove(int key) {
52+
if (contains(key)) {
53+
list.remove(new Pair(key, get(key)));
54+
}
55+
}
56+
}
57+
58+
private class Pair {
59+
private final int key;
60+
private int value;
61+
62+
Pair(int key, int value) {
63+
this.key = key;
64+
this.value = value;
65+
}
66+
67+
@Override
68+
public boolean equals(Object o) {
69+
if (this == o) return true;
70+
if (o == null || getClass() != o.getClass()) return false;
71+
Pair pair = (Pair) o;
72+
return key == pair.key && value == pair.value;
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(key, value);
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)