[Leetcode] 706. Design HashMap

2021. 1. 19. 15:45Problem Solving/LeetCode

Leetcode 706.Design HashMap

https://leetcode.com/problems/design-hashmap/

 

Design HashMap - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Solution

파이썬의 대표적인 해시형 자료형인 딕셔너리이용

class MyHashMap:

    def __init__(self):
        self.dict = {}

    def put(self, key: int, value: int) -> None:
        self.dict[key] = value

    def get(self, key: int) -> int:
        if not key in self.dict:
            return -1
        else:
            return self.dict.get(key)

    def remove(self, key: int) -> None:
        if key in self.dict:
            del self.dict[key]

        # Your MyHashMap object wisll be instantiated and called as such:
        # obj = MyHashMap()
        # obj.put(key,value)
        # param_2 = obj.get(key)
        # obj.remove(key)

이게 Accept될 줄은 몰랐는데.. 일단 통과는 되었다.

파이썬의 내장되어있는 딕셔너리를 사용하면 더 빠르긴 하지만, 문제가 의도하는 정석적인 풀이는

 

- 개별 체이닝 방식 (파이썬 알고리즘 인터뷰 p.296)

 

일 것이다.

 

 

Reference

파이썬 알고리즘 인터뷰

파이썬 알고리즘 인터뷰
국내도서
저자 : 박상길
출판 : 책만 2020.07.15
상세보기

 

반응형