Skip to content

Commit

Permalink
added Two Sum problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 authored Nov 26, 2022
1 parent 6c2a2dd commit 22c1263
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Hashing/Two Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> mp;
vector<int> res;
for(int i=0;i<nums.size();i++){
if(mp[target-nums[i]]>0){
res.push_back(mp[target-nums[i]]-1);
res.push_back(i);
break;
}
mp[nums[i]] = i+1;
}
return res;
}
};

0 comments on commit 22c1263

Please sign in to comment.