일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 알고리즘
- IOS
- 코딩테스트
- Trie
- dft
- 전자공학
- 파이썬
- backjoon
- 컨볼루션
- leet code
- 백준
- PYTHON
- 트라이
- 신호처리
- 알고리즘문제풀이
- SWIFT
- 릿코드
- Leet Coding Challenge
- 이산신호처리
- DTFT
- leetcode
- 코테
- 독서노트
- DSP
- 알고리즘 문제풀이
- 스위프트
- 카카오 코딩테스트
- 프로그래머스
- SWIFTUI
- 코테준비
Archives
- Today
- Total
매일 매일 성장하는 섭섭군
[Leet Coding Challenge] Design HashSet, 2020.08.01~07 본문
반응형
영어공부도 할겸 알고리즘 문제를 LeetCode 에서도 풀어보기로 했다.
그 중 Leet Coding Challenge라는 것이 있어 매주 도전하면 좋을것 같다는 생각이 든다.
처음 풀어본 문제는 다소 쉬운 문제다. 해시를 구현하는 것인데 백준이나 프로그래머스 등에서 해시를 사용하여 문제를 푸는 것이 아닌 그냥
해시를 구현하라! 라는 문제이다. 문제는 다음과 같다.
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
해시에 값을 추가하거나 삭제, 해당 값이 존재하는지를 살피면 된다.
전체 풀이 코드는 다음과 같다.
class MyHashSet:
def __init__(self):
self.myDict = [False]*1000001
def add(self, key: int) -> None:
self.myDict[key] = True
def remove(self, key: int) -> None:
if self.contains(key) :
self.myDict[key] = False
def contains(self, key: int) -> bool:
return self.myDict[key]
ps) 문제 조건에 hash-set 라이브러리를 사용하지 말라고 했는데 사용해도 통과되긴 한다. 다음은 라이브러리를 사용했을 때이다.(dict 사용)
class MyHashSet:
def __init__(self):
self.myDict = dict()
def add(self, key: int) -> None:
self.myDict[key] = True
def remove(self, key: int) -> None:
if self.contains(key) :
del self.myDict[key]
def contains(self, key: int) -> bool:
if key in self.myDict :
return True
else :
return False
반응형
'알고리즘 문제풀이 > LeetCode' 카테고리의 다른 글
[Leet Coding Challenge]Excel Sheet Column Number, 2020.08.08~14 (0) | 2020.08.11 |
---|---|
[Leet Coding Challenge] Add and Search Word - Data structure design, 2020.08.01~07 (0) | 2020.08.06 |
[LeetCode Top Interview Questions] Happy Number (0) | 2020.08.05 |
[Leet Coding Challenge] Power of FourSolution, 2020.08.01~07 (0) | 2020.08.04 |
[Leet Coding Challenge] Valid Palindrome, 2020.08.01~07 (0) | 2020.08.04 |
Comments