-
Notifications
You must be signed in to change notification settings - Fork 5
/
solution.cpp
46 lines (40 loc) · 1.06 KB
/
solution.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
46
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> record;
for (int i = 0; i < nums.size(); i++) {
int num = target - nums[i];
if (record.find(num) != record.end()) {
return vector<int>{record[num], i};
}
record[nums[i]] = i;
}
return vector<int>{-1, -1};
}
};
struct Example {
vector<int> nums;
int target;
vector<int> ans;
};
int main() {
vector<Example> examples = {
{{2, 7, 11, 15}, 9, {0, 1}},
{{3, 2, 4}, 6, {1, 2}},
{{3, 3}, 6, {0, 1}},
};
Solution *solve = new Solution();
for (int i = 0; i < examples.size(); i++) {
vector<int> ans = solve->twoSum(examples[i].nums, examples[i].target);
if (ans == examples[i].ans) {
cout << "PASS: CASE " << i << endl;
} else {
cout << "FAIL: CASE " << i << endl;
}
}
return 0;
}