/
fluffymax2005
/
leetcode-solutions
Обзор
Документация
Войти
/
fluffymax2005
/
leetcode-solutions
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
two-sum/main.cpp
38 строк
780 B
fluffymax2005
First commit with gitignore + some solved problems
09 мар 2026, 21:39
09 мар 2026, 21:39
08aa95e
Код
Авторство
О чём код?
#include <iostream> #include <map> #include <ostream> #include <vector> // https://leetcode.com/problems/two-sum/description/ class Solution { public: std::vector<int> twoSum(std::vector<int>& nums, int target) { std::map<int, int> map; // num -> index for (int i = 0; i < nums.size(); ++i) { long long dif = target - nums[i]; const auto it = map.find(dif); if (it != map.end()) { return {it->second, i}; } map[nums[i]] = i; } return {}; } }; int main() { Solution sol; std::vector<int> twoSum{0,4,3,0}; std::vector<int> indexes = sol.twoSum(twoSum, 0); for (const auto& index : indexes) { std::cout << index << std::endl; } }