Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 자연어처리
- 편스토랑
- 금융문자분석경진대회
- gs25
- 캐치카페
- 데이콘
- PYTHON
- ubuntu
- 프로그래머스 파이썬
- Docker
- 편스토랑 우승상품
- Kaggle
- leetcode
- 맥북
- 파이썬
- ChatGPT
- 프로그래머스
- 코로나19
- programmers
- 백준
- Baekjoon
- dacon
- Git
- Real or Not? NLP with Disaster Tweets
- hackerrank
- 더현대서울 맛집
- AI 경진대회
- SW Expert Academy
- 우분투
- github
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 705. Design HashSet (Python) 본문
728x90
반응형
Design a HashSet without using any built-in hash table libraries.
To be specific, your design should include these functions:
- add(value): Insert a value into the HashSet.
- contains(value) : Return whether the value exists in the HashSet or not.
- remove(value): Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
Example:
MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)
Note:
- All values will be in the range of [0, 1000000].
- The number of operations will be in the range of [1, 10000].
- Please do not use the built-in HashSet library.
Solution
class MyHashSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self.my_hash = []
def add(self, key: int) -> None:
if key not in self.my_hash:
self.my_hash.append(key)
def remove(self, key: int) -> None:
if key in self.my_hash:
self.my_hash.remove(key)
def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
if key in self.my_hash:
return True
return False
# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 645. Set Mismatch (Python) (0) | 2020.09.19 |
---|---|
[leetCode] 191. Number of 1 Bits (Python) (0) | 2020.09.18 |
[leetCode] 1356. Sort Integers by The Number of 1 Bits (Python) (0) | 2020.09.16 |
[leetCode] 1550. Three Consecutive Odds (Python) (0) | 2020.09.15 |
[leetCode] 1365. How Many Numbers Are Smaller Than the Current Number (Python) (2) | 2020.09.14 |
Comments