-
Notifications
You must be signed in to change notification settings - Fork 776
/
2Sum.cpp
28 lines (28 loc) · 817 Bytes
/
2Sum.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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// brute force approach
// for(int i=0;i<nums.size();i++){
// for(int j=i+1;j<nums.size();j++){
// if(target==nums[i]+nums[j]){
// return {i,j};
// }
// }
// }
// return {};
// Leetcode Question
// Below Approach is optimised code
unordered_map<int,int> numIndex;
for(int i=0;i<nums.size();i++){
int complement=target-nums[i];
if(numIndex.find(complement)!=numIndex.end()){
return {numIndex[complement],i};
}
numIndex[nums[i]]=i;
}
return {};
}
};