-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-1-Two_Sum.cpp
49 lines (41 loc) · 962 Bytes
/
leetcode-1-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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* -- Id = 1 -- */
#include <cstdlib>
#include <iostream>
#include <vector>
#include <map>
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::map<int, int> myMap;
for (size_t i = 0; i < nums.size(); ++i)
{
myMap[nums[i]] = i;
}
for (size_t i = 0; i < nums.size(); ++i)
{
if(myMap.find(target-nums[i]) != myMap.end() &&
myMap[(target-nums[i])] != i)
{
std::vector<int> v;
v.push_back(i);
v.push_back(myMap[(target-nums[i])]);
return v;
}
}
std::vector<int> v;
v.push_back(555);
return v;
}
int main(int argc, char const *argv[])
{
std::vector<int> a;
a.push_back(2);
a.push_back(7);
a.push_back(11);
a.push_back(15);
std::vector<int> v = twoSum(a, 9);
for (auto i : v)
{
std::cout << i << " ";
}
std::cout << std::endl;
return EXIT_SUCCESS;
}