Skip to content

Commit

Permalink
Added Design HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Oct 4, 2023
1 parent d55e9aa commit d490a01
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,15 @@ add_executable(MaximumTwinSumOfALinkedList
target_link_libraries(
MaximumTwinSumOfALinkedList
GTest::gtest_main
)

# Design HashMap
add_executable(DesignHashMap
Easy/DesignHashMap/include/solution.hpp
Easy/DesignHashMap/tests/test.cpp
)

target_link_libraries(
DesignHashMap
GTest::gtest_main
)
49 changes: 49 additions & 0 deletions Easy/DesignHashMap/include/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>

class MyHashMap {
public:
MyHashMap() = default;

static auto hash(const int32_t x) -> int32_t {
return (2 * x + 97) % 30011;
}

auto put(const int32_t key, const int32_t value) -> void {
int32_t i = hash(key);
for (auto &[k, v] : _table[i]) {
if (key == k) {
v = value;
return;
}
}
_table[i].emplace_back(key, value);
}

auto get(const int32_t key) -> int32_t {
int32_t i = hash(key);
for (auto &[k, v]: _table[i]) {
if (k == key) return v;
}
return -1;
}

auto remove(const int32_t key) -> void {
int32_t i = hash(key);
for (auto it = _table[i].begin();
it != _table[i].end(); ++it) {
if ((*it).first == key) {
_table[i].erase(it);
return;
}
}
}

private:
std::list<std::pair<int32_t,int32_t>> _table[30011];
};
15 changes: 15 additions & 0 deletions Easy/DesignHashMap/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../include/solution.hpp"

auto main() -> int {
MyHashMap *myHashMap = new MyHashMap();
myHashMap->put(1, 1);
myHashMap->put(2, 2);
myHashMap->get(1);
myHashMap->get(3);
myHashMap->put(2, 1);
myHashMap->get(2);
myHashMap->remove(2);
myHashMap->get(2);

return 0;
}
20 changes: 19 additions & 1 deletion Medium/MaximumTwinSumOfALinkedList/include/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ struct ListNode {
class Solution {
public:
static auto pair_sum(ListNode *head) -> int32_t {
if (head->next == nullptr) return head->value;
if (head->next->next == nullptr) {
return head->value + head->next->value;
}


}

static auto insert(ListNode **head_ref,
const int32_t new_value) -> void {
auto *node = new ListNode();
node->value = new_value;
node->next = (*head_ref);
(*head_ref) = node;
}

static auto insert(ListNode **head_ref)
static auto print(ListNode *head) -> void {
while (head != nullptr) {
std::cout << head->value << ' ';
head = head->next;
}
}
};

0 comments on commit d490a01

Please sign in to comment.