Home Design HashSet
Post
Cancel

Design HashSet

Leetcode Problem

Design HashSet

hashset을 사용하여 add, remove, contains 함수를 구현하는 문제입니다.

1
2
3
4
5
6
7
8
9
10
class MyHashSet:
    def __init__(self):
        self.hash = {}
    def add(self, key: int) -> None:
        self.hash[key] = 1
    def remove(self, key: int) -> None:
        if key in self.hash:
            del self.hash[key]
    def contains(self, key: int) -> bool:
        return True if key in self.hash else False

python의 dictionary와 hash가 같으므로 dictionary를 활용했습니다.





참고

This post is licensed under CC BY 4.0 by the author.