-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsrc.cpp
More file actions
29 lines (29 loc) · 682 Bytes
/
src.cpp
File metadata and controls
29 lines (29 loc) · 682 Bytes
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
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int, int> m;
vector<int> result;
for (size_t i = 0; i < numbers.size(); ++i) {
if (m.find(numbers[i]) == m.end()) {
m[target - numbers[i]] = i;
} else {
result.push_back(m[numbers[i]] + 1); // index from 1, not 0
result.push_back(i + 1); // index from 1
}
}
return result;
}
};
int main(int argc, char **argv)
{
Solution solution;
vector<int> numbers = {2,7,11,15};
auto result = solution.twoSum(numbers, 9);
printf("%d\n%d\n", result[0], result[1]);
return 0;
}