-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001twoSum.cpp
More file actions
50 lines (43 loc) · 985 Bytes
/
001twoSum.cpp
File metadata and controls
50 lines (43 loc) · 985 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// Created by Pasco on 16/4/1.
//
#include "catch.hpp"
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> res;
map<int,int> mp;
int find;
for (int i = 0; i < numbers.size() ; ++i) {
find = mp[target-numbers[i]];
if(find){
res.push_back(find);
res.push_back(i);
break;
}
mp[numbers[i]] = i;
}
return res;
}
};
TEST_CASE("two sum case 1") {
/*
* case 1
* */
Solution solution;
vector<int> fooArray = {0, 1, 2, 3, 4, 5, 6, 7};
int target = 13;
/*
* expected output and actual output
* */
vector<int> expectedOutput = {6,7};
vector<int> actualOutput = solution.twoSum(fooArray,target);
/*
* assert
* */
REQUIRE(expectedOutput == actualOutput);
}