-
Notifications
You must be signed in to change notification settings - Fork 15
/
two_sum_code.cpp
37 lines (33 loc) · 928 Bytes
/
two_sum_code.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int , vector<int>> mp;
for(int i=0;i<nums.size();i++){
mp[nums[i]].push_back(i);
}
for(int i=0;i<nums.size();i++){
int t = target - nums[i];
if(mp.find(t)!=mp.end()){
if(nums[i]==t){
if(mp[nums[i]].size()>1){
return {mp[nums[i]][0], mp[nums[i]][1]};
}
}
else{
return {mp[nums[i]][0], mp[t][0]};
}
}
}
return {};
}
};
int main() {
vector<int> nums = {2,7,11,15};
int target = 9;
Solution obj;
vector<int> ans = obj.twoSum(nums,target);
cout<<ans[0]<<" "<<ans[1]<<endl;
return 0;
}