leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE | |
commit | 9516fefe56c16201672ae03ddc5a6eada9d3859b |
parent | e1480481481aa22552204b859c9fcc0354b78d2d |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Tue, 30 May 2023 09:05:01 +0200 |
Daily Problem
Diffstat:A | Problems/0705.cpp | | | ++++++++++++++++++++++++++++++++++++++++++ |
M | README.md | | | + |
2 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/Problems/0705.cpp b/Problems/0705.cpp
@@ -0,0 +1,42 @@
class MyHashSet {
class Node {
public:
int val;
Node *next;
Node(): val(-1), next(nullptr) {}
Node(int val, Node *next): val(val), next(next) {}
};
static constexpr int SIZE = 10007;
Node bucket[SIZE] = {};
int hash(int key) {
return key % SIZE;
}
public:
MyHashSet() {}
void add(int key) {
if(contains(key)) return;
Node *b = &bucket[hash(key)];
b->next = new Node(key, b->next);
}
void remove(int key) {
Node *prev = &bucket[hash(key)], *crnt = prev->next;
while(crnt && crnt->val != key) {
prev = crnt;
crnt = crnt->next;
}
if(!crnt) return;
prev->next = crnt->next;
delete crnt;
}
bool contains(int key) {
for(Node *crnt = bucket[hash(key)].next; crnt; crnt=crnt->next)
if(crnt->val == key) return true;
return false;
}
};
diff --git a/README.md b/README.md
@@ -324,6 +324,7 @@ for solving problems.
| 0701 | Medium | [Insert into a Binary Search Tree](Problems/0701.cpp) |
| 0703 | Easy | [Kth Largest Element in a Stream](Problems/0703.cpp) |
| 0704 | Easy | [Binary Search](Problems/0704.cpp) |
| 0705 | Easy | [Design HashSet](Problems/0705.cpp) |
| 0706 | Easy | [Design HashMap](Problems/0706.cpp) |
| 0707 | Medium | [Design Linked List](Problems/0707.cpp) |
| 0713 | Medium | [Subarray Product Less Than K](Problems/0713.cpp) |