-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0001_two_sum.cpp
31 lines (28 loc) · 998 Bytes
/
0001_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
#include <stdio.h>
#include <utility>
#include <unordered_map>
#include <vector>
#include <string>
class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
// key: target - encountered num, value: index of encountered num
std::unordered_map<int, int> num_pos;
int num_items = nums.size();
for (int i=0; i < num_items; i++) {
int curr_num = nums[i];
// If the current number is not in the map
if (num_pos.count(curr_num) == 0) {
int sec_num = target-curr_num;
num_pos.insert(std::make_pair(sec_num, i));
}
// If the current number is in the map, you found the answer
else {
return {num_pos[curr_num], i};
}
}
// If we go through the entire for loop without returning the answer
// it means there was no valid pair
return std::vector<int>();
}
};