-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_two_sum.cpp
37 lines (30 loc) · 859 Bytes
/
1_two_sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <map>
#include "my_utils.h"
std::vector<int> twoSum(std::vector<int> &nums, int target)
{
std::map<int, int> seen{{target - nums[0], 0}};
for (int i = 1; i < nums.size(); i++)
{
if (auto it = seen.find(nums[i]); it != seen.end())
{
return {it->second, i};
}
seen.insert({target - nums[i], i});
}
return {0, 0};
}
int main()
{
int t{9};
std::vector<int> n{2, 7, 11, 15};
std::cout << "nums: " << n <<" target: " << t << " -> output: " << twoSum(n, t) << std::endl;
t = 6;
n = {3,2,4};
std::cout << "nums: " << n <<" target: " << t << " -> output: " << twoSum(n, t) << std::endl;
t = 6;
n = {3,3};
std::cout << "nums: " << n <<" target: " << t << " -> output: " << twoSum(n, t) << std::endl;
return 0;
}