-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTwo Sum.cpp
42 lines (30 loc) · 1.03 KB
/
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
/*
Solution by Rahul Surana
***********************************************************
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,pair<int,int>> m;
vector<int> ans;
int i = 0;
for(auto x: nums){
if(m[target-x].first != 0){
ans.push_back(m[target-x].second);
ans.push_back(i);
return ans;
}
else{
m[x] = make_pair(1,i);
}
// cout << x <<" "<< m[x].second <<"\n";
i++;
}
return vector<int>();
}
};