-
Notifications
You must be signed in to change notification settings - Fork 0
/
1 Two Sum.cpp
62 lines (53 loc) · 1.79 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
/*
int n=nums.size();
int i=0,j=n-1;
// store in pairand the sort the pair with resepect to the first then do accordingkly fuck off
unordered_map<int,in>mp; // pair
for(int k=0;k<n;k++){
mp[nums[k]]=k; // unique since only one answer is present
}
sort(nums.begin(),nums.end()); // O(nlogn), need to sort for finding pair sum
while(i<j){
int value=nums[i]+nums[j]; // current pair sum
if(value==target){
return {mp[nums[i]].second,mp[nums[j]].second}; // since there is exactly one output
// ++i;
// --j;
}
else if(value>target){
--j;
}
else{
++i;
}
}
return {};
*/
// Understand this solution
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < nums.size(); i++) {
int numberToFind = target - nums[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind]);
result.push_back(i);
return result;
}
//number was not found. Put it in the map.
hash[nums[i]] = i;
}
return result;
}
};