[LeetCode/Python] 706. Design HashMap
2025. 4. 10. 19:10
99클럽 코테 스터디 | 비기너 | 9일차
https://leetcode.com/problems/design-hashmap/description/
접근 방법
1. __init__() 함수에서 HashMap의 정보를 저장할 array self.HashMap을 선언한다.
2. put() 함수에서 self.HashMap의 pair 중 입력 데이터 key와 동일한 값을 가진 key가 이미 존재하면 value 값을 업데이트, 일치하지 않으면 새로운 [key, value] 쌍을 추가한다.
3. get()함수에서 self.HashMap의 pair 중 입력 데이터 key와 동일한 값을 가진 데이터가 있으면 해당 key의 vaule를 리턴, 없으면 -1을 리턴한다.
4. remove() 함수에서 self.HashMap의 pair 중 입력 데이터 key와 동일한 값을 가진 데이터가 있으면 해당 pair를 삭제한다.
class MyHashMap:
def __init__(self):
self.HashMap = []
def put(self, key: int, value: int) -> None:
for pair in self.HashMap:
if pair[0] == key:
pair[1] = value
return
self.HashMap.append([key, value])
def get(self, key: int) -> int:
for pair in self.HashMap:
if pair[0] == key:
return pair[1]
return -1
def remove(self, key: int) -> None:
for pair in self.HashMap:
if pair[0] == key:
self.HashMap.remove(pair)
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode/Python] 1464. Maximum Product of Two Elements in an Array (0) | 2025.04.10 |
---|---|
[LeetCode/Python] 2283. Check if Number Has Equal Digit Count and Digit Value (0) | 2025.04.09 |
[LeetCode/Python] 70. Climbing Stairs (0) | 2025.04.07 |
[LeetCode/Python] 225. Implement Stack using Queues (0) | 2025.04.04 |
[LeetCode/Python] 232. Implement Queue using Stacks (0) | 2025.04.03 |