-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
34 lines (32 loc) · 738 Bytes
/
main.cpp
File metadata and controls
34 lines (32 loc) · 738 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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map <int,int> mymap;
for(int i=0; i < nums.size(); i++){
if(mymap.count(target - nums[i]))
return {mymap.find(target - nums[i])->second, i};
mymap.insert({nums[i],i});
}
}
};
int main()
{
int a[4] = {2,7,11,15};
vector<int> nums(a,a+4);
vector<int> r;
int target = 9;
Solution s;
r = s.twoSum(nums,target);
int len = r.size();
for(int i=0; i<len; i++){
printf("%d ",r[i]);
}
return 0;
}