https://leetcode.com/problems/design-hashmap/description/
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
- MyHashMap() initializes the object with an empty map.
- void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value.
- int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
- void remove(key) removes the key and its corresponding value if the map contains the mapping for the key.
Example 1:
Input
["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"]
[[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]]
Output
[null, null, null, 1, -1, null, 1, null, -1]
Explanation
MyHashMap myHashMap = new MyHashMap();
myHashMap.put(1, 1); // The map is now [[1,1]]
myHashMap.put(2, 2); // The map is now [[1,1], [2,2]]
myHashMap.get(1); // return 1, The map is now [[1,1], [2,2]]
myHashMap.get(3); // return -1 (i.e., not found), The map is now [[1,1], [2,2]]
myHashMap.put(2, 1); // The map is now [[1,1], [2,1]] (i.e., update the existing value)
myHashMap.get(2); // return 1, The map is now [[1,1], [2,1]]
myHashMap.remove(2); // remove the mapping for 2, The map is now [[1,1]]
myHashMap.get(2); // return -1 (i.e., not found), The map is now [[1,1]]
Constraints:
- 0 <= key, value <= 106
- At most 104 calls will be made to put, get, and remove.
1. Python
풀면서도 이게 맞나 싶은 풀이법..
class MyHashMap:
def __init__(self):
self.k = []
self.v = []
def put(self, key: int, value: int) -> None:
if key not in self.k:
self.k.append(key)
self.v.append(value)
else:
idx = self.k.index(key)
self.v[idx] = value
def get(self, key: int) -> int:
if key not in self.k : return -1
else:
idx = self.k.index(key)
return self.v[idx]
def remove(self, key: int) -> None:
if key in self.k:
idx = self.k.index(key)
self.k.pop(idx)
self.v.pop(idx)
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
아래는 정석 풀이법
class MyHashMap:
def __init__(self):
self.size = 1000
self.map = [[] for _ in range(self.size)]
def put(self, key: int, value: int) -> None:
hash_key = key % self.size
for pair in self.map[hash_key]:
if pair[0] == key:
pair[1] = value
return
self.map[hash_key].append([key, value])
def get(self, key: int) -> int:
hash_key = key % self.size
for pair in self.map[hash_key]:
if pair[0] == key:
return pair[1]
return -1
def remove(self, key: int) -> None:
hash_key = key % self.size
for pair in self.map[hash_key]:
if pair[0] == key:
self.map[hash_key].remove(pair)
return
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key, value)
# param_2 = obj.get(key)
# obj.remove(key)
2. C
typedef struct {
int *key;
} MyHashMap;
MyHashMap* myHashMapCreate() {
MyHashMap* ret = (MyHashMap*)malloc(sizeof(MyHashMap));
ret->key = (int*)malloc(sizeof(int) * 1000001);
memset(ret->key, -1, sizeof(int) * 1000001);
return ret;
}
void myHashMapPut(MyHashMap* obj, int key, int value) {
obj->key[key] = value;
}
int myHashMapGet(MyHashMap* obj, int key) {
return obj->key[key];
}
void myHashMapRemove(MyHashMap* obj, int key) {
obj->key[key] = -1;
}
void myHashMapFree(MyHashMap* obj) {
free(obj->key);
obj->key = NULL;
free(obj);
obj = NULL;
}
아.. 진짜 짜아승난다!
여기까지..
'Coding_Practice' 카테고리의 다른 글
Adding Spaces to a String[M,ArrayTwo Pointers,String,Simulation] (0) | 2024.07.26 |
---|---|
Reverse Linked List[E,Linked List,Recursion] (0) | 2024.07.26 |
Design Circular Deque[M,Array,Linked List,Design,Queue] (0) | 2024.07.26 |
Linked List Cycle(E,Hash Table,Linked List,Two Pointers) (0) | 2024.07.26 |
Removing Minimum Number of Magic Beans[M,Array,Greedy,Sorting,Enumeration,Prefix Sum] (0) | 2024.07.26 |