Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/two-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "two-sum.hpp"
#include <unordered_map>

bool two_sum(
const int nums[ARRAY_SIZE],
const int target,
std::size_t& index0,
std::size_t& index1
) {
std::unordered_map < int, std::size_t > container;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не компилируется: https://github.com/czertyaka/CppDevCourse-hw3/actions/runs/12244079113/job/34439743584?pr=38

Забыли подключить заголовочный файл для использования std::unordered_map

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

понял, исправлю

// Объявляет хэш-таблицу и создает пустой контейнер.
for (std::size_t i = 0; i < ARRAY_SIZE; ++i) {
int complement = target - nums[i];
if (container.find(complement) != container.end()) {
index0 = container[complement];
index1 = i;
return true;
}
container[nums[i]] = i;
}
return false;
}
/*Использовал хэш таблицу для сокращения асимптотической сложности до O(n),
так как поиск в хэш-таблице выполняется за O(1)*/