-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo-sum.cpp
36 lines (29 loc) · 889 Bytes
/
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
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
unordered_map<int, int> hash;
vector<int> output;
for (int i = 0; i < nums.size(); i++)
{
int toFind = target - nums[i];
// so there is only one solution that means as soon as we find a solution
// we must return what we got
if (hash.find(toFind) != hash.end())
{
output.push_back(i);
output.push_back(hash[toFind]);
cout << hash[toFind] << " and index rn " << i;
break;
}
// must store the values in the hashtable after a pass to avoid finding a duplicate
hash[nums[i]] = i;
}
return output;
}
};