-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtwo-sum.cpp
More file actions
36 lines (31 loc) · 795 Bytes
/
two-sum.cpp
File metadata and controls
36 lines (31 loc) · 795 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
35
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> m;
int n = nums.size();
for(int i = 0; i < n ; i++){
int temp = target - nums[i];
if(m[temp] > 0){
return {m[temp] - 1,i};
}
m[nums[i]] = i + 1;
}
return {-1,-1};
}
};
// updated solution of two sum leetcode
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int>mpp;
for(int i=0;i<nums.size();++i)
{
if(mpp.find(target-nums[i])!=mpp.end())
{
return {mpp[target-nums[i]],i};
}
mpp[nums[i]] = i;
}
return {};
}
};