Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b7eca88

Browse files
committed
new
1 parent eba58dc commit b7eca88

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

‎arrays_hashing/design_hashmap.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Design HashMap
3+
4+
Design a HashMap without using any built-in hash table libraries.
5+
6+
Implement the MyHashMap class:
7+
8+
- MyHashMap() initializes the object with an empty map.
9+
- void put(int key, int value) inserts a (key, value) pair into the HashMap.
10+
If the key already exists in the map, update the corresponding value.
11+
- int get(int key) returns the value to which the specified key is mapped,
12+
or -1 if this map contains no mapping for the key.
13+
- void remove(key) removes the key and its corresponding value if the map
14+
contains the mapping for the key.
15+
16+
Example 1:
17+
18+
Input:
19+
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
20+
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
21+
22+
Output:
23+
[null, null, null, 1, -1, null, 1, null, -1]
24+
25+
Explanation:
26+
MyHashMap myHashMap = new MyHashMap();
27+
myHashMap.put(1, 1); # The map is now [[1,1]]
28+
myHashMap.put(2, 2); # The map is now [[1,1], [2,2]]
29+
myHashMap.get(1); # return 1
30+
myHashMap.get(3); # return -1 (not found)
31+
myHashMap.put(2, 1); # update the existing value -> [[1,1], [2,1]]
32+
myHashMap.get(2); # return 1
33+
myHashMap.remove(2); # remove the mapping for 2 -> [[1,1]]
34+
myHashMap.get(2); # return -1 (not found)
35+
36+
Constraints:
37+
0 <= key, value <= 1,000,000
38+
At most 10,000 calls will be made to put, get, and remove.
39+
"""
40+
41+
# Method 1:
42+
class MyHashMap:
43+
44+
def __init__(self):
45+
self.size = 1000001
46+
self.data = [-1 for _ in range(self.size)]
47+
48+
def put(self, key: int, value: int) -> None:
49+
self.data[key] = value
50+
51+
def get(self, key: int) -> int:
52+
return self.data[key]
53+
54+
def remove(self, key: int) -> None:
55+
self.data[key] = -1
56+
57+
# Method 2:
58+
class MyHashMap:
59+
60+
def __init__(self):
61+
self.size = 10000
62+
self.data = [[] for _ in range(self.size)]
63+
64+
def calculate_hash_value(self,key: int):
65+
return key % self.size
66+
67+
def put(self, key: int, value: int) -> None:
68+
hv = self.calculate_hash_value(key)
69+
for i, (k,v) in enumerate(self.data[hv]):
70+
if k == key:
71+
self.data[hv][i] = (key,value)
72+
return
73+
self.data[hv].append((key,value))
74+
75+
def get(self, key: int) -> int:
76+
hv = self.calculate_hash_value(key)
77+
for k,v in self.data[hv]:
78+
if k == key:
79+
return v
80+
return -1
81+
82+
def remove(self, key: int) -> None:
83+
hv = self.calculate_hash_value(key)
84+
self.data[hv] = [(k,v) for (k,v) in self.data[hv] if k != key]
85+

‎arrays_hashing/design_hashset.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,3 @@ def remove(self, key: int) -> None:
9494
def contains(self, key: int) -> bool:
9595
hv = self.calculate_hash_value(key)
9696
return key in self.table[hv]
97-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /