-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_twoSum.cpp
45 lines (34 loc) · 923 Bytes
/
1_twoSum.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
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <unordered_map>
static std::vector<int>
twoSum(std::vector<int>& nums, int target) {
int size = nums.size();
// brute force
// for (int i = 0; i != size; ++i) {
// for (int j = i + 1; j != size; ++j) {
// if (target == (nums[i] + nums[j]))
// return {i, j};
// }
// }
// map
std::unordered_map<int, int> containor;
for (int i = 0; i != size; ++i) {
int tmp = target - nums[i];
if (containor.end() != containor.find(tmp)) {
return {containor[tmp], i};
}
containor.insert(std::make_pair(nums[i], i));
}
return {0};
}
int
main(int argc, char const *argv[])
{
std::vector<int> array = {2, 7, 11, 16};
std::vector<int> res = twoSum(array, 9);
for (auto i : res) {
std::cout << array[i] << std::endl;
}
return 0;
}