Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Емельянов Максим Сергеевич #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Owner

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
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)*/